[英]C macros, what's the meaning of ((void)0)?
Given the following code written according to the C99 standard:给定以下根据 C99 标准编写的代码:
#define LOW 1
#define MEDIUM 2
#define HIGH 3
#define LOGGING_LEVEL HIGH
#if LOGGING_LEVEL >= MEDIUM
#define LOG_MEDIUM(message) printf(message)
#else
#define LOG_MEDIUM(message) ((void)0)
#endif
void load_configuration() {
//...
LOG_MEDIUM("Configuration loaded\n");
}
what's the purpose of ((void)0)
I searched the web a lot but nothing found regarding this. ((void)0)
的目的是什么,我搜索了 web 很多,但没有找到任何相关信息。
Plus, why didn't we wrote ;
另外,我们为什么不写
;
after using printf(message)
使用
printf(message)
后
Main idea is to exclude all LOG_MEDIUM
if the criteria was not meet.主要思想是如果不满足条件,则排除所有
LOG_MEDIUM
。 After compilation those calls will not affect functionality.编译后这些调用不会影响功能。
The void-cast fixes a compiler warning. void-cast 修复了编译器警告。 Here's an analogous testcase:
这是一个类似的测试用例:
int main(void)
{
0; // generates "foo.c:3:2: warning: statement with no effect"
(void)0;
return 0;
}
and (using a script to add gcc's warning flags) you see a warning for the line without a cast:并且(使用 脚本添加 gcc 的警告标志)您会看到没有强制转换的行的警告:
$ gcc-stricter -c foo.c
foo.c: In function ‘main’:
foo.c:3:2: warning: statement with no effect [-Wunused-value]
0;
^
The extra parentheses and lack of semicolon allow the macro's result to be used interchangeably with the printf
.额外的括号和缺少分号允许宏的结果与
printf
互换使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.