C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 15)
15.
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;
}
5
9
10
3+7
Answer: Option
Explanation:

The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.

Step 1: int x; The variable x is declared as an integer type.

Step 2: x = MAX(3+2, 2+7, 3+7); becomes,

=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)

=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )

=> x = (5 >9 ? (10): (10) )

=> x = 10

Step 3: printf("%d\n", x); It prints the value of 'x'.

Hence the output of the program is "10".

Discussion:
4 comments Page 1 of 1.

Monika said:   1 decade ago
We have evaluate from the right side, (a>b?a:b) if the condition is true then it will take the value of a or else it will take b value.

Here, 9>10 is wrong so its takes the value 10. Then 5>10 is also wrong So again it takes the value 10.

Abhimanyu said:   1 decade ago
@Nandu.

The statement will the pointer will not go to the true section of ternary operator in case of (5>9) So the the pointer will go in (9>10) and again in false section and answer will be 10.

Gowtham said:   2 years ago
@Nandu.

It's like true or false.
(5 >9 ? (10): (10))
If (5>9)
Printf(first 10\n");
Else
Printf(second10\n");
So finally x=10;

Nandu said:   1 decade ago
I can't understand the step two, the statement "(5>10?:10)" has become (10).

How is it possible?

Post your comments here:

Your comments will be displayed after verification.