//预处理器在编译之前选择代码:
//另一个分支根本不会被编译
#define DEBUG 1
#define VERSION 3
#if DEBUG
const char *mode = "debug";
#else
const char *mode = "release";
#endif
//mode is "debug"
//#if 计算整数表达式,#elif 相当于其中的 "else if"
#if VERSION >= 3
int features = 30;
#elif VERSION == 2
int features = 20;
#else
int features = 10;
#endif
//features is 30
//#error 带着消息终止构建,
//这里只在条件为 true 时发生
#if VERSION < 2
#error "VERSION 2 or newer is needed"
#endif
//#ifdef 和 #ifndef 只询问名称是否已定义;
//defined() 在 #if 内部作用相同,
//C23 增加了 #elifdef 和 #elifndef
#ifdef LOGGING
bool logging = true;
#else
bool logging = false;
#endif
//logging is false
//编译器会定义一些自己的名称,
//代码借此选择平台(沙盒运行 Linux)
#if defined(_WIN32)
const char *platform = "Windows";
#elif defined(__APPLE__)
const char *platform = "macOS";
#elif defined(__linux__)
const char *platform = "Linux";
#else
const char *platform = "unknown";
#endif
//platform is "Linux"
//#if 0 可以关闭一段代码,哪怕其中带有注释
#if 0
printf("this line is not compiled\n"); /* 编译器根本看不到 */
#endif
printf("mode is %s\n", mode);
printf("features is %d\n", features);
printf("logging is %s\n", logging ? "true" : "false");
printf("platform is %s\n", platform);