在C中使用#define没有值 [英] Using #define in C with no value

查看:243
本文介绍了在C中使用#define没有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用的#define没有值,例如

If a #define is used with no value, like

#define COMMAND_SPI()

默认情况下它取值为0吗?

does it take value 0 by default?

推荐答案

否,结果为空.从字面上看,该符号什么也没有替换.

No, it evaluates to nothing. Literally the symbol gets replaced with nothing.

但是,一旦有了#define FOO,预处理器的条件#ifdef FOO现在将为true.

However, once you have #define FOO, the preprocessor conditional #ifdef FOO will now be true.

还请注意,在gcc和可能的其他编译器中,如果在命令行上使用-DFOO定义了宏,则默认情况下其值为1.

Note also that in gcc and possibly other compilers, if you define a macro with -DFOO on the command line, that evaluates to 1 by default.

自从OP更新了他的问题以引用类似函数的宏以来,让我们考虑一个小例子.

Since the OP updated his question to reference function-like macros, let's consider a small example.

#define FOO
#define BAR()


FOO
BAR
BAR()

这不是有效的C程序,但是预处理程序无关. 如果使用gcc -E Input.c进行编译,则会得到一个空格,然后是BAR,然后是另一个空格.这是因为第一个和第三个表达式的计算结果为空,并且中间的表达式未展开,因为其后没有().

This is not a valid C program, but the preprocessor does not care. If I compile this with gcc -E Input.c, I get a blank, followed by BAR followed by another blank. This is because the first and third expressions evaluate to nothingness, and the middle expression is not expanded because there are no () after it.

这篇关于在C中使用#define没有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆