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 2 of 2.
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.
So it becomes:
k = ((++i)>(j++))?(++i):(j++);
And that's why I will increment 2 time.
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?
Anybody please expalin?
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
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.
Hence j is incremented only once.
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?
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.
If the corresponding j is printed in the next; so the j value becomes 6.
Biswajit said:
1 decade ago
I become 12 and k become 12 but why not j become 7 ?
Srinivas said:
1 decade ago
Sir in the above program hw the value of 'i' became 12..?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers