The Preprocessor
# se shuru hone wali har line preprocessor handle karta hai, ek alag pass jo actual compiler ke aapka 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 use hota hai wahan textually substitute ho jaata hai, apne argument ke saath substitute hoke — SQUARE(5), compilation shuru hone se pehle ((5) * (5)) ban jaata 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 — unme 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 bina, SQUARE(1 + 2) expand hoke 1 + 2 * 1 + 2 ban jaata, (1 + 2) * (1 + 2) nahi. Is site ki kisi bhi doosri language mein aisa kuch nahi hai: ye genuinely ek alag, textual layer hai actual C language ke neeche.
Aage: bitwise operators — individual bits ko directly manipulate karna.
Practice exercise
Ek macro SQUARE(x) define karo jo ((x) * (x)) mein expand ho, phir SQUARE(9) print karo.