Govt. Exams
Entrance Exams
#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.
When a macro is redefined, the new definition replaces the old one. Some compilers may issue a warning about redefinition.
Angle brackets search in standard system directories. Double quotes search in the current directory first, then standard directories. This is the standard convention.
Preprocessor directives can appear in any order in the source code. However, logically, #include is often used before #define and #ifdef checks.
#ifdef checks if a macro identifier is currently defined. Code following it is compiled only if the macro is defined.
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
The macro expands to 5+3*2 = 5+6 = 11, not 16. However, due to operator precedence, it becomes 5+3*2. Actually, it expands correctly to (5+3)*2 = 16 when evaluated.