A preprocessor directive is a line starting with #. Directives are not C statements: they end at the end of the line and are handled before compilation. The main ones are #include, #define / #undef, #if / #ifdef / #ifndef / #else / #elif / #endif, #error, #pragma, and #line.
Remember
#include <stdio.h> /* paste a header */
#define NAME value /* define a macro */
#undef NAME /* remove a macro */
#ifdef NAME ... #endif /* conditional blocks */
#error "message" /* stop compilation */
Reference
Cheat Sheet
Keep these facts handy while you work with Preprocessor Directives in C.
Topic
Detail
Start
# as first non-space character
End
End of line (use \ to continue)
Scope
From definition to end of file or #undef
Inspect
gcc -E file.c
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: #include Directive.
Example 1 — define, ifdef, and undef
The preprocessor decides which printf lines exist.
#define DEBUG defines the macro with an empty value.
#ifdef DEBUG is true, so the first message is compiled in.
After #undef, #ifndef DEBUG is true.
Edge Cases & Pitfalls
Check these before relying on Preprocessor Directives in real programs.
space
Spaces before the hash
Leading whitespace is allowed, but nothing else can precede # on the line.
cont
Line continuation
A backslash must be the very last character on the line; trailing spaces break it.
Remember
Key Takeaways
Lines: directives are line-oriented.
Order: processed top to bottom.
Debug: use gcc -E.
One line: A preprocessor directive is a line starting with #.
Frequently Asked Questions
No. A directive ends at the end of its line. A stray semicolon becomes part of the replacement text.
Before compilation. It transforms the source text, and the compiler then sees the result.
Continue with "C #include Directive" next. Paste the contents of a header file into your source file. The C Preprocessor hub lists every topic in order.