ProgrammingHard
#define ADD(a, b) a + b printf("%d", ADD(1, 2) * 3); What is printed?
- A. 7
- B. 9
- C. 5
- D. Compile error: a macro cannot appear inside an expression
Correct answer: A. 7
A macro is pure text substitution, so ADD(1, 2) * 3 becomes 1 + 2 * 3 and * binds first: 7. 9 assumes the macro behaves like a function returning 3, and 5 applies the * 3 to a instead of b; the fix is #define ADD(a, b) ((a) + (b)).
More C Programming questions
- char grid[2] = {"ab", "cd"}; printf("%c", grid[1][0]); What is printed?
- struct S { int a; char c; } arr[2] = {{1}}; printf("%d", arr[1].a); What is printed?
- int a[5] = {[2] = 7, 9}; printf("%d", a[3]); What is printed?
- char s = "hi"; s++; printf("%s", s); What is printed?
- int x = 1; { int x = 2; printf("%d", x); } printf("%d", x); What is printed?
- printf("%d", (int)'A' + 'x01'); What is printed?