Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output generated by the following code?

#define square(a) (a*a)
printf("%d", square(4+5));

  • A.81
  • B.4
  • C.29
  • D.None of the above.

Answer: C

Correct answer: 29

The macro has no parentheses round its parameter, so the argument is pasted in as raw text:

printf("%d", (4+5*4+5));

Multiplication binds tighter than addition, giving \(4 + 20 + 5 = 29\) rather than the intended \(9^2 = 81\). Parenthesising the parameter - #define square(a) ((a)*(a)) - is what fixes it.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.