C Preprocessor Introduction
In this page:
#include <header.h>
#define NAME value
#ifdef NAME
// compiled only if NAME is defined
#endif
What is the Preprocessor?
This is a purely textual process -- the preprocessor has no understanding of C syntax or semantics, it just performs pattern-based find-and-replace and file-inclusion operations before the real compiler ever sees the code.
Example: What is the Preprocessor?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro GREETING (replaced by the preprocessor)
#define GREETING "Hello"
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf(GREETING);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Header Expansion
This is effectively a copy-paste operation: the entire target header file's text is inserted in place of the #include line, which is why including the same header twice (without protection) can cause duplicate-definition errors.
Example: Header Expansion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("stdio.h contents are copied in before compiling");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Macro Substitution
Because this is pure text substitution rather than a function call, macros have no type checking and can behave unexpectedly with complex arguments -- this is a common source of subtle bugs compared to using an actual function.
Example: Macro Substitution
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%.2f", PI);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Conditional Preparation
This lets you compile different versions of the same source file for different platforms or configurations, without maintaining separate copies of the code, by defining or omitting certain macros at compile time.
Example: Conditional Preparation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro DEBUG (replaced by the preprocessor)
#define DEBUG 1
// Program execution starts in main()
int main() {
#if DEBUG
// Print formatted text to the screen
printf("Debug mode");
#endif
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The Compilation Flow
This intermediate expanded output (sometimes visible with a compiler flag like -E) contains no macros, comments, or conditional blocks left -- just plain C code ready for the compiler's lexer and parser to process.
Example: The Compilation Flow
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro VALUE (replaced by the preprocessor)
#define VALUE 5
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", VALUE);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Thinking the preprocessor runs after compilation.
- Putting semicolons after preprocessor directives.
- Forgetting that macros are text substitution.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: