#define prn(a) printf("%d",a)
#define print(a,b,c) prn(a), prn(b), prn(c)
#define max(a,b) (a<b)? b:a
main()
{
int x=1, y=2;
print(max(x++,y),x,y);
print(max(x++,y),x,y);
}
output:222342
pls explain how we get output 342 at end
#define prn(a) printf("%d",a)
#define print(a,b,c) prn(a), prn(b), prn(c)
#define max(a,b) (a<b)? b:a
main()
{
int x=1, y=2;
print(max(x++,y),x,y);
print(max(x++,y),x,y);
}
output:222342
pls explain how we get output 342 at end
The result is undefined behavior because of the ++ operator. max() evaluates both variables a and b twice, so the ++ operator gets executed twice. Exactly what the final result will be is compiler implementation dependent. Your compiler may produce one value while another compiler may produce something else.
#include<stdio.h>
#define prn(a) printf("%d",a)
#define print(a,b,c) prn(a), prn(b), prn(c)
#define max(a,b) (a<b)? b:a
int main()
{
int x=1, y=2;
printf("First Print Call :");
print(max(x++,y),x,y); // here x is incremented after the max call returns then value of x becomes 2(remember we used x++ not ++x so the max call wil recieve 1 as x's(a) value and 2 as y's value (b) so the output becomes prn(y),prn(x),prn(y) ....Therefore the output is 222
printf("\n2nd print call : ");
prn(max(x++,y));// Now here the value of x=2,y = 2 call = max(x++,y) here a(x++) is not < b(y) therefore it returns a(x++ = x+1) the #define max statement increments x once in the call also the it returns x then x is incremented and we get 3 as output
prn(x);
prn(y);
printf("\n");
}
Your code was not so impressive so i made some changes for better understanding..
a bit tricky part is here
prn(max(x++,y));
What max call does is that:-
#define max(a,b) (a<b)? b:a
So the max call here gets input 2 integers...x++ and y
as a is not less that b the max returns a which is x++
Here x increments one time...Its value becomes 3...
Now after the max call the x++ in the parenthesis increments now the value of x becomes 4...So the output 4...y's value is 2 so output=2
I hope you find it little bit easy now... Mess with your code ...Try with different variables and you'll get a better understanding of this....
Use code tags...or someone will vote you down...