Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Post increment in macro 2

Status
Not open for further replies.

Sac123

Programmer
Jul 24, 2003
3
IN
#define MAX(a,b) (a>b?a:b)

void main(void)
{
int a = 3, b=4;

printf("%d %d %d\n",a,b,MAX(a++,b++));
}

I expected the answer for this as
4 5 4
but answer i got is
4 6 5
Can anyone explain me why is this so...
 
at the first is executed (a++ > b++ ? a : b)
after that is passed to printf;


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
but according to ur explaination it should print
4 5 4.
can you plz elaborate why b is getting incremented twice
 
The order of evaluation is undefined in this instance. Actually, the macro would have been

(a++ > b++? a++: b++)

a is incremented twice that is probably why you are getting 5.
 
This is a nice illustration on why macros should be avoided.


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top