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, c) (a>b ? a>c ? a : c: b>c ? b : c)

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

 

  • A.3
  • B.5
  • C.10
  • D.3+7

Answer: C

Correct answer: 10

The macro expands to a nested conditional. With \(a = 3+2 = 5\), \(b = 2+7 = 9\) and \(c = 3+7 = 10\):

(5>9 ? … : 9>10 ? 9 : 10)

The first test fails, so the else branch is taken; \(9 > 10\) also fails, leaving 10. Addition binds tighter than the comparisons, so the unparenthesised arguments happen to be safe here.

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

Post your comment

Please log in or register to post a comment.