Reputation: 33
#define AD(x,y) (x+y)
int main()
{
int x1=5,y1=2,z1;
int x2=5,y2=2,z2;
z1 = AD(x1,++y1);
z2 = (x2+++y2) ;
printf("%d %d %d\n",x1,y1,z1);
printf("%d %d %d\n",x2,y2,z2);
}
why the output is different? the first case is : 5 3 8 and the second is : 6 2 7
Upvotes: 3
Views: 72
Reputation: 310980
This expression
z2=x2+++y2;
is parsed by the compiler like
z2 = x2++ + y2;
From the C Standard (6.4 Lexical elements)
4 If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.
So these tokens +++
are parsed like ++
and +
.
The expression with the macro
z1=AD(x1,++y1);
is parsed by the compiler like
z1 = x1 + ++y1;
The compiler already formed these sets of tokens x1
and ++y1
due to the comma between the tokens.
So these two statements are different.
Upvotes: 5