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;
}
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.
Vithu said:
4 years ago
How does the value of k become 12? Explain please.
Julio said:
5 years ago
Thanks @Jay.
Nadhira KC said:
7 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.
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)
Narasio said:
7 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?
But in this x will inc by one.
Tell me how it will work?
Sundri said:
7 years ago
k=MAN(12,6)
O/P =( i=12,j=6)
And k=12 no?
Because rightmost
Answer 12 6 6.
O/P =( i=12,j=6)
And k=12 no?
Because rightmost
Answer 12 6 6.
Praneetha said:
8 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.
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.
Jerin said:
8 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!
Sreenivasulu reddy said:
8 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.
Could you please reply me.
Jyothi said:
9 years ago
When i incremented in condition j should also increment but why it is not incremented -((++i)>(j++)).
(1)
Ssh said:
9 years 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?
++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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers