代码点读机 / C / #if

#if C · 预处理

如果常量表达式的值为真(非0),就保留它和对应 #endif 之间的代码参与编译。

怎么用

#if 常量表达式
  代码
#endif

小例子

#include <stdio.h>
#define LEVEL 2
int main(void) {
#if LEVEL > 1
    printf("高级版");
#endif
    return 0;
}

运行结果

输出 高级版

提醒:条件必须是编译期能算出的常量表达式,不能是运行时才确定的变量

官方文档:https://en.cppreference.com/w/c/preprocessor/conditional

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C全部词条