如何获得包含变量的#if预处理器条件,而不会产生错误?

时间:2015-06-01 11:18:07

标签: c c-preprocessor

包含编译时变量的#if预处理程序阶段语句如何在预处理程序阶段自行解析?

以下代码运行时没有任何错误:

#include<stdio.h> 

void main()
{
   int num=10; /* compile time */

#if((num%2)==0)  /* #if is preprocessor stage but has num of compile time why not error here? */
   printf("\nNumber is Even");
#else
   printf("\nNumber is Odd");
#endif 
}

1 个答案:

答案 0 :(得分:11)

对于#if中的评估,预处理器会通过适当的扩展替换已定义的宏的所有标识符。之后保留的所有标识符都具有值0