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 1 of 2.

Nadhira KC said:   8 years ago
k = ((++i)>(j++)) ? (++i):(j++);
When the condition ((++i)>(j++)) is executed the value of i becomes 11 and 6 respectively. Since the condition is true, the first part of the conditional expression is alone executed (++i) and hence i becomes 12 and the value of j remains 6.

Thus the output is 12, 6, 12.
(1)

Spl said:   1 decade ago
In the ternary operator ?: the second part never gets executed hence the only the j++ 1st j++ (the one present in condition) is executed and the other one (that is in else part) never gets executed .

Hence j is incremented only once.

Praneetha said:   9 years ago
If(condition) (true):(false);

According to above syntax:
If(11>5)?12:6;
When 11>5 is true then 12 is displayed.
If the condition is false then 6 is displayed.

I hope you understand.

Dileep kumar Kotha said:   1 decade ago
The #define has a semi-colon in the end, and hence two semi-colons in the same statement, when the macro gets replaced. Results in garbage value for K. Hence D is the right answer

Jerin said:   9 years ago
To my best knowledge, the macro passes value after computation of increment, and not the expression!; but the solution considers passing of the whole expression. I'm confused!

Jay said:   1 decade ago
MAN(++i, j++) //this line will replace by MAN(x, y) ((x)>(y)) ? (x):(y);

So it becomes:

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

And that's why I will increment 2 time.

Ssh said:   1 decade ago
In MAN(++i, j++);

++i and j++ are expressions so they need to be executed first which results into MAN (11, 5).

How can an expression be passed to a function?

Arjun said:   1 decade ago
I still didn't get why 'j' should not be 7 when we finally print it. I agree it is 6 when we assign k, but shouldnt we see 7 when we print it in the next line?

Sreenivasulu reddy said:   9 years ago
The value of I value which is incremented is assigned to k, then how the value if I will be 12 instead of 11 sir.

Could you please reply me.

Kotesh said:   1 decade ago
The post increment (++) operator increments the j to 7 only when.

If the corresponding j is printed in the next; so the j value becomes 6.


Post your comments here:

Your comments will be displayed after verification.