Govt Exams
The preprocessor is a separate program that processes source code before the compiler. It handles directives like #include, #define, and #ifdef.
Function-like macros can cause issues due to multiple evaluation of arguments and operator precedence problems. Modern C prefers inline functions and const for safety.
#ifdef checks if a macro is defined (exists). #if evaluates a constant integer expression (can check macro values, compare numbers, etc.).
#define MIN(a,b) ((a)
The macro expands to ((x++)<(y++)?(x++):(y++)). In the condition, x++ returns 5 and increments x to 6. Since 5<10 is true, x++ is evaluated again (x becomes 7, but z gets 6). Actually, this shows side effects problem - x becomes 7, y stays 10.
#define DOUBLE(x) x*x
int result = DOUBLE(2+3);
The macro expands to 2+3*2+3 = 2+6+3 = 11, not 25. This is because x is not parenthesized. Should use #define DOUBLE(x) ((x)*(x))
Macros are purely textual substitutions performed by the preprocessor at compile time, not at runtime. They don't allocate memory themselves.
#define STR(x) #x
printf("%s", STR(hello));
The # operator (stringification) converts the macro argument into a string literal. STR(hello) becomes "hello" as a string.
The ## operator (token pasting) concatenates two tokens into a single token during macro expansion.
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
int x=5, y=10;
SWAP(x,y);
printf("%d %d", x, y);
The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.
#undef is used to remove the definition of a macro so it cannot be used in subsequent code.