A quick syntax reference for the C Preprocessor (cpp), which can be invoked independently or within C/C++ compilation toolchains.
Standalone Preprocessing
Run the preprocessor directly without invoking the compiler:
# -P inhibits generation of linemarkers
cpp -P source.c > preprocessed.c
Header Inclusions
#include <stdio.h> /* System / standard library headers */
#include "my_header.h" /* Local project headers */
Macro Definitions
#define BUFFER_SIZE 1024
#define PI 3.1415926535
/* Undefine a macro */
#undef BUFFER_SIZE
Function-Like Macros
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define DEG_TO_RAD(x) ((x) * 0.0174532925)
Stringification (#)
The single # operator converts macro parameters into string literals:
#define STR(name) #name
#define PRINT_VAR(x) printf("%s = %d\n", #x, x)
char *var_name = STR(my_object); /* → "my_object" */
Token Concatenation (##)
The double ## operator glues two tokens together into a single token:
#define COMMAND(NAME) { #NAME, NAME ## _command }
struct command commands[] = {
COMMAND(quit), /* → { "quit", quit_command } */
COMMAND(help) /* → { "help", help_command } */
};
Conditional Compilation
#ifdef DEBUG
#define LOG(msg) printf("[DEBUG] %s\n", msg)
#elif defined(VERBOSE)
#define LOG(msg) printf("[INFO] %s\n", msg)
#else
#define LOG(msg) /* noop */
#endif
/* Include guards */
#ifndef MY_HEADER_H
#define MY_HEADER_H
/* declarations here */
#endif /* MY_HEADER_H */
Diagnostics & Warnings
#if __STDC_VERSION__ < 201112L
#warning "C11 features are not fully enabled"
#endif
#if !defined(REQUIRED_CONFIG)
#error "REQUIRED_CONFIG must be defined before compilation"
#endif
Standard Predefined Macros
__FILE__: Current source filename string.__LINE__: Current line number integer.__DATE__: Compilation date string ("Mmm dd yyyy").__TIME__: Compilation time string ("hh:mm:ss").