The Preprocessor
# se shuru hone wali har line preprocessor handle karta hai, ek alag pass jo asli compiler ke aap ka code dekhne se pehle run hota hai. #include <stdio.h> literally us header file ka content wahan paste kar deta hai; #define SQUARE(x) ((x) * (x)) ek macro define karta hai jo jahan bhi istemal hota hai wahan textually substitute ho jata hai, apne argument ke sath substitute hoke — SQUARE(5), compilation shuru hone se pehle ((5) * (5)) ban jata hai.
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define MAX_USERS 100
int main() {
printf("%d\n", SQUARE(5));
printf("%d\n", MAX_USERS);
return 0;
}25 100
Macros functions nahi hain — un mein koi type checking aur koi real parameters nahi hote, bas text substitution hota hai — isiliye SQUARE(x) ko extra parentheses mein wrap kiya gaya hai: inke baghair, SQUARE(1 + 2) expand hoke 1 + 2 * 1 + 2 ban jata, (1 + 2) * (1 + 2) nahi. Is site ki kisi bhi doosri language mein aisa kuch nahi hai: ye waqai ek alag, textual layer hai asli C language ke neeche.
Aage: bitwise operators — individual bits ko directly manipulate karna.
Practice exercise
Ek macro SQUARE(x) define karein jo ((x) * (x)) mein expand ho, phir SQUARE(9) print karein.