C Preprocessor

Quick reference for the C macro preprocessor, which can be used independent of C/C++.

A cheatsheet by @rstacruz|Refreshed 7 months ago.Refresh|View source on Github

Compiling

Compiling

$ cpp -P file > outfile

Includes

Includes

#include "file"

Defines

Defines

#define FOO
#define FOO "hello"

#undef FOO

If

If

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

Error

Error

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

Macro

Macro

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

Token concat

Token concat

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

file and line

file and line

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