Cheat Sheet速查表
跳转到主要内容

C 预处理器 速查表

本文根据 Devhints / C Preprocessor 翻译。代码示例及版本范围沿用原文。

……的快速参考 C 宏预处理器,它可独立于 C/C++ 使用。

参考资料

编译

$ cpp -P file > outfile

包含 (Includes)

#include "file"

定义 (Defines)

#define FOO
#define FOO "hello"

#undef FOO

条件 (If)

#ifdef DEBUG
  console.log('hi');
#elif defined VERBOSE
  ...
#else
  ...
#endif

错误 (Error)

#if VERSION == 2.0
  #error Unsupported
  #warning Not really supported
#endif

宏

#define DEG(x) ((x) * 57.29)

标记连接 (Token concatenation)

#define DST(name) name##_s name##_t
DST(object);   #=> object_s object_t;

字符串化 (Stringification)

#define STR(name) #name
char * a = STR(object);   #=> char * a = "object";

文件与行号

#define LOG(msg) console.log(__FILE__, __LINE__, msg)
#=> console.log("file.txt", 3, "hey")