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

Discussion

What will be the output of the program?

#include <stdio.h>
#define MAX(a, b) (a > b ? a : b)

int main()
{
    int x;
    x = MAX(3+2, 2+7);
    printf("%d\n", x);
    return 0;
}

 

  • A.8
  • B.9
  • C.6
  • D.4

Answer: B

Correct answer: 9

The macro body parenthesises nothing, so the expansion is (3+2 > 2+7 ? 3+2 : 2+7). Addition binds tighter than >, so the comparison is \(5 > 9\), which is false, and the result is \(2+7 = 9\). Here the missing parentheses happen not to matter, but with an argument such as 1&2 they would.

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

Post your comment

Please log in or register to post a comment.