C Programming - C Preprocessor - Discussion

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;
}

[A]. 12, 6, 12
[B]. 11, 5, 11
[C]. 11, 5, Garbage
[D]. 12, 6, Garbage

Answer: Option A

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


Srinivas said: (Apr 12, 2011)  
Sir in the above program hw the value of 'i' became 12..?

Biswajit said: (Jul 22, 2011)  
I become 12 and k become 12 but why not j become 7 ?

Kotesh said: (Aug 19, 2011)  
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.

Arjun said: (Aug 23, 2011)  
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?

Spl said: (Aug 30, 2011)  
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.

Dileep Kumar Kotha said: (Oct 23, 2011)  
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

Annanya said: (Jan 27, 2012)  
Why the value of I will get incremented by two. As only the increment ++i is given.

Anybody please expalin?

Jay said: (Sep 20, 2012)  
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: (Aug 21, 2015)  
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?

Jyothi said: (Jan 13, 2016)  
When i incremented in condition j should also increment but why it is not incremented -((++i)>(j++)).

Sreenivasulu Reddy said: (Jun 29, 2016)  
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.

Jerin said: (Aug 9, 2016)  
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!

Praneetha said: (Dec 18, 2016)  
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.

Sundri said: (Sep 8, 2017)  
k=MAN(12,6)

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

Narasio said: (Sep 11, 2017)  
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?

Nadhira Kc said: (Dec 24, 2017)  
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.

Julio said: (Jun 27, 2019)  
Thanks @Jay.

Vithu said: (Jan 30, 2021)  
How does the value of k become 12? Explain please.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.