🟡 Intermediate · Lesson 30
Preprocessor Directives
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
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
Preprocessor क्या है?
C preprocessor compilation से पहले run होता है। यह # से start होने वाली lines handle करता है जैसे #include, #define, #ifdef आदि।
💡 याद रखें
Preprocessor directives के अंत में semicolon नहीं लगाया जाता।
#include
#include header file का content program में add करता है।
C Language
#include <stdio.h> #include "myheader.h"
#define और Macros
C Language
#define PI 3.14159 #define SQUARE(x) ((x) * (x)) #define MAX(a,b) ((a) > (b) ? (a) : (b))
⚠️ ध्यान दें
Macros में parentheses जरूर लगाएं, वरना calculation गलत हो सकती है।
Conditional Compilation
C Language
#define DEBUG
#ifdef DEBUG
printf("Debug mode on\n");
#endifInclude Guards
C Language
#ifndef MYHEADER_H #define MYHEADER_H int add(int a, int b); #endif
सारांश
- Preprocessor compiler से पहले run होता है
#includeheader file जोड़ता है#defineconstants/macros बनाता है#ifdefconditional compilation करता है- Include guards duplicate include रोकते हैं