C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 1)
1.
What will be the output of the program?
#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
    int i=10, j=5, k=0;
    k = MAN(++i, j++);
    printf("%d, %d, %d\n", i, j, k);
    return 0;
}
12, 6, 12
11, 5, 11
11, 5, Garbage
12, 6, Garbage
Answer: Option
Explanation:

The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.

Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.

Step 2: k = MAN(++i, j++); becomes,

=> k = ((++i)>(j++)) ? (++i):(j++);

=> k = ((11)>(5)) ? (12):(6);

=> k = 12

Step 3: printf("%d, %d, %d\n", i, j, k); It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.

Hence the output of the program is 12, 6, 12

Discussion:
18 comments Page 2 of 2.

Narasio said:   8 years ago
If the condition is true the x will print otherwise y,
But in this x will inc by one.

Tell me how it will work?

Annanya said:   1 decade ago
Why the value of I will get incremented by two. As only the increment ++i is given.

Anybody please expalin?

Jyothi said:   10 years ago
When i incremented in condition j should also increment but why it is not incremented -((++i)>(j++)).
(1)

Sundri said:   8 years ago
k=MAN(12,6)

O/P =( i=12,j=6)
And k=12 no?
Because rightmost
Answer 12 6 6.

Srinivas said:   1 decade ago
Sir in the above program hw the value of 'i' became 12..?

Biswajit said:   1 decade ago
I become 12 and k become 12 but why not j become 7 ?

Vithu said:   5 years ago
How does the value of k become 12? Explain please.

Julio said:   6 years ago
Thanks @Jay.


Post your comments here:

Your comments will be displayed after verification.