这些编译器指令有什么区别?
这些指令之间有什么区别(如果有的话)?
#ifdef FOO
#if defined FOO
#if defined(FOO)
我正在使用 CCS 编译器,但我也对其他 C 编译器感兴趣。
What, if anything, is the difference between these directives?
#ifdef FOO
#if defined FOO
#if defined(FOO)
I'm using the CCS compiler, but I'm interested in other C compilers as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,
#if Defined
的主要用途是检查一行上的多个宏定义。否则,对于单个宏定义条件,据我所知,它们是相同的。另外,上述程序可以使用
gcc -Wall -ansi ac
正常编译,因此表明#if Defined
是正确的 ANSI C。此外,1987 年的 ANSI C 摘要 列出了#if Defined
作为新定义的行为ANSI 标准下的预处理器——这应该是您将使用的任何符合 ANSI 的编译器的标准。如果您没有使用
#if Defined
,则还必须执行C 预处理器的 Redhat 手册 说
From what I've seen, the main use for
#if defined
is to do a check for multiple macro definitions on one line. Otherwise, for single macro definition conditionals, they are identical as far as I know.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 doAlso, the Redhat manual for the C preprocessor says
根据 C99 标准,所有 3 种形式完全相同且有效。一般来说,#ifdef 是首选,因为它比其他两种形式短,但在某些情况下您可能希望使用其他形式:
如果测试多个符号或多个符号的定义复杂表达式:
如果有多个子句,使用
#elif Defined(...) 会容易得多
比#else
、#ifdef
:define
中括号的使用是可选的;我更喜欢使用它们来增加清晰度。All 3 forms are exactly equivalent and valid, according to the C99 standard. Generally
#ifdef
is preferred because it is shorter than the other two forms, but there are situations where you would want to use the other forms:If testing for the definitions of multiple symbols, or more complex expressions:
If you have multiple clauses, it's much easier to use
#elif defined(...)
than#else
,#ifdef
:The usage of parentheses with
defined
is optional; I prefer using them to add clarity.#ifdef 是 #if Defined 的缩写,我认为两者都不需要括号,所以基本上它们是相同的。
来自 Turbo C,我习惯于查看 #ifdef 而不是 #if Defined
#ifdef is short for #if defined and I think you don't need parenthesis on neither, so basically they are the same.
Coming from Turbo C, I'm used to looking at #ifdef rather than #if defined
最初只有
#ifdef
,但是当#if
出现时,就必须有define()
才能实现#if
为#ifdef
的超集。Originally there was only
#ifdef
, but when#if
came along it was necessay to havedefined()
in order for#if
to superset#ifdef
.