C Preprocessor Cheat Sheet

From WikiOD

Here is the cheatsheat of C Preprocessor and basic guide on how to use C Preprocessor

Reference[edit | edit source]

{: .-three-column}

Compiling[edit | edit source]

$ cpp -P file > outfile

Includes[edit | edit source]

#include "file"

Defines[edit | edit source]

#define FOO
#define FOO "hello"

#undef FOO

If[edit | edit source]

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

Error[edit | edit source]

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

Macro[edit | edit source]

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

Token concat[edit | edit source]

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

Stringification[edit | edit source]

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

file and line[edit | edit source]

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

Credit:rstacruz