🟡 Intermediate · Lesson 30
Preprocessor Directives
What is Preprocessor?
The C preprocessor runs before compilation. It handles lines starting with #, such as #include, #define, #ifdef and #ifndef.
💡 Remember
Preprocessor directives do not end with semicolon.
#include Directive
#include adds the content of a header file into the program.
C Language
#include <stdio.h> // standard library header #include "myheader.h" // user-defined header file
#define and Macros
#define is used to create constants and macros.
C Language
#include <stdio.h>
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
int main() {
printf("Area = %.2f\n", PI * SQUARE(5));
printf("Max = %d", MAX(10, 20));
return 0;
}Area = 78.54
Max = 20
⚠️ Macro Safety
Always use parentheses in function-like macros to avoid priority errors.
Conditional Compilation
C Language
#include <stdio.h>
#define DEBUG 1
int main() {
int x = 10;
#ifdef DEBUG
printf("Debug: x = %d\n", x);
#endif
printf("Program running");
return 0;
}Include Guards
Include guards prevent the same header file from being included multiple times.
myheader.h
#ifndef MYHEADER_H #define MYHEADER_H void greet(); int add(int a, int b); #endif
Common preprocessor directives
Here are the directives you will meet most often, all beginning with # and processed before compilation:
| Directive | Purpose |
|---|---|
#include | Insert the contents of a header file |
#define | Create a constant or macro |
#undef | Remove a previously defined macro |
#ifdef / #ifndef | Compile a block only if a macro is (not) defined |
#if / #else / #endif | Conditional compilation based on a constant expression |
#pragma | Give special instructions to the compiler |
Common mistakes
- Putting a semicolon after a directive, e.g.
#define PI 3.14;— the;becomes part of the value. - Forgetting parentheses in a macro, so
SQUARE(a+b)expands wrongly. - Using a macro where a
constor real function would be safer and type-checked. - Leaving out include guards, causing the same header to be included twice.
Summary
- Preprocessor runs before compiler
#includeadds header files#definecreates constants and macros- Conditional compilation helps in debug and platform-specific code
- Include guards prevent duplicate inclusion
👉 Go deeper
This lesson covers preprocessor basics. For function-like macros, macro pitfalls and the #/## operators in detail, see Macros in C.
Frequently Asked Questions
What is the preprocessor in C?
The preprocessor is a step that runs before the actual compilation. It handles every line that starts with
# — such as #include and #define — by textually editing your source code (pasting in headers, replacing constants) before the compiler ever sees it.Why do preprocessor directives not end with a semicolon?
Because they are not C statements — they are instructions to the preprocessor, not to the compiler. A line like
#define PI 3.14 is a text-substitution rule, so it ends at the line break, and adding a semicolon would accidentally become part of the replacement text.What is the difference between #include <file> and #include "file"?
Angle brackets
< > tell the preprocessor to look for the header in the standard system directories, used for library headers like stdio.h. Double quotes tell it to look in your own project folder first, used for headers you wrote yourself.What are include guards and why are they needed?
Include guards are the
#ifndef / #define / #endif pattern that stops the same header from being pasted in more than once. Without them, including a header twice can cause duplicate-definition errors, so guards make headers safe to include repeatedly.What is conditional compilation?
Conditional compilation uses directives like
#ifdef and #endif to include or skip pieces of code depending on whether a macro is defined. It is commonly used to add debug-only code or to write code that adapts to different platforms.💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.