如果有的话,这些指令之间有什么区别?
#ifdef FOO
#if defined FOO
#if defined(FOO)
我正在使用 CCS 编译器,但我对其他 C 编译器也很感兴趣。
最佳答案
据我所知,#if defined
的主要用途是检查一行上的多个宏定义。否则,对于单个宏定义条件,据我所知,它们是相同的。
#include <stdio.h>
int main()
{
#if defined(FOO) && defined(BAR)
printf("foobar!\n");
#else
printf("nothing.\n");
#endif
return 0;
}
$ tcc -DFOO -run a.c nothing. $ tcc -DBAR -run a.c nothing. $ tcc -DFOO -DBAR -run a.c foobar!
Also, the above program compiles fine with gcc -Wall -ansi a.c
so that suggests #if defined
is correct ANSI C. Moreover, this ANSI C summary from 1987 lists #if defined
as newly defined behavior for the preprocessor under ANSI standards -- this should be standard across any ANSI-compliant compiler you will use.
If you weren't using #if defined
, you'd have to do
#ifdef FOO
#ifdef BAR
printf("foobar!\n");
#endif /* BAR */
#endif /* FOO */
另外,the Redhat manual for the C preprocessor 说
关于c - 这些编译器指令之间有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1518168/