#define MAX 100
#define MIN 50
#undef MAX
#define MAX 200
int main() {
printf("%d", MAX);
return 0;
}
What will be the output of this program?
The #undef directive undefines the previously defined macro MAX (which was 100). Then MAX is redefined as 200. So printf will output 200. The #undef allows redefining macros without compilation errors.
#define CONCAT(a,b) a##b
int CONCAT(var,1) = 10;
What is the actual variable name created?
The ## operator (token pasting) concatenates tokens directly. CONCAT(var,1) produces var1 as the variable name, not a##b.
#define STR(x) #x
printf(STR(Hello World));
The # operator (stringification) converts the macro argument into a string literal. STR(Hello World) becomes "Hello World", which printf prints with quotes.
__VA_ARGS__ allows macros to accept a variable number of arguments. Example: #define PRINT(...) printf(__VA_ARGS__) enables flexible argument passing.
#define ADD(x,y) ((x)+(y))
#define MULTIPLY(x,y) ADD(x,y)*ADD(x,y)
What is MULTIPLY(2,3)?
MULTIPLY(2,3) expands to ADD(2,3)*ADD(2,3) = ((2)+(3))*((2)+(3)) = 5*5 = 25. Wait, recalculating: (5)*(5)=25. The answer should be 25, but given options, 36=(2+3)²is if computed as (2+3)*(2+3) with y ignored. Actual answer: ((2)+(3))*((2)+(3))=5*5=25. Correcting: answer is A=25, not B.
#define SQUARE(x) x*x
int result = SQUARE(5+3);
Without parentheses, SQUARE(5+3) expands to 5+3*5+3, which evaluates to 5+15+3=23 due to operator precedence, not (5+3)*(5+3)=64. This demonstrates why macro parameters need parentheses.
int main() { int arr[DOUBLE(5)]; ... }
What is the size of the array?
The macro DOUBLE(5) expands to (2*(5)) = 10 at preprocessing time. Array declarations require compile-time constant expressions, and macro-expanded constants qualify. The array has 10 elements. This works because the macro creates a constant expression that the compiler can evaluate.
__VA_ARGS__ (C99 feature) represents all variable arguments after the required parameters. It can be empty if no extra arguments are provided. It must be used with '...' in the macro definition. C89/C90 don't support variadic macros.
#define A B
#define B 5
printf("%d", A);
What is printed?
The preprocessor expands A to B in the first pass. In the second pass, it recognizes B as a macro and expands it to 5. Nested macro expansion is allowed and the final result is 5. The preprocessor performs multiple expansion passes as needed.
#error is standard C and generates a compilation error. #pragma message is commonly supported for warnings. #warn and #warning are non-standard. These are useful for version checking and compile-time notifications.