Code:#include<stdio.h>
#define MAX(x,y) ( x ) > ( y ) ? x:y
main()
{ int i=10,j=5,k=0;
k= MAX(i++,++j);
printf("%d %d %d ",i,j,k);
}
Ans:12 6 11
but i'm expecting value of i to be 11 can some one explain how it is 12 in this expression?
Printable View
Code:#include<stdio.h>
#define MAX(x,y) ( x ) > ( y ) ? x:y
main()
{ int i=10,j=5,k=0;
k= MAX(i++,++j);
printf("%d %d %d ",i,j,k);
}
Ans:12 6 11
but i'm expecting value of i to be 11 can some one explain how it is 12 in this expression?
This is a classic case of macro expansion versus function call - since the macro expands the input parameter twice in the MAX() macro, it will increment one of the values twice.
--
Mats
when compiling this line of code:
the compiler substitutes MAX with the macro you defined:Code:k= MAX(i++,++j);
Code:k= ( i++ ) > ( ++j ) ? i++ : ++j