Preprocessor Directives in C
In this lesson: Learn C preprocessor directives — #include, #define, macros, #ifdef, #ifndef, #pragma — with examples and output. Hindi & English.
What is Preprocessor?
The C preprocessor runs before compilation. It handles lines starting with #, such as #include, #define, #ifdef and #ifndef.
Preprocessor directives do not end with semicolon.
#include Directive
#include adds the content of a header file into the program.
#include <stdio.h> // standard library header #include "myheader.h" // user-defined header file
#define and Macros
#define is used to create constants and macros.
#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;
}Always use parentheses in function-like macros to avoid priority errors.
Conditional Compilation
#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.
#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
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?
# — 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?
#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"?
< > 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?
#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?
#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.