-
Unary operators
Hello to all,
I've lost touch with C programming for almost 3 years, so I forgot some subtle details.
I was solving one problem by writing C program and was surprised to learn that this code:
Code:
int main(void)
{
int x=3, y=2;
printf("%d", (x++));
printf("\n%d", (--y));
printf("\n%d", (++x));
printf("\n%d", (y--));
return 0;
}
is producing same result as this one:
Code:
int main(void)
{
int x=3, y=2;
printf("%d", x++);
printf("\n%d", --y);
printf("\n%d", ++x);
printf("\n%d", y--);
return 0;
}
How to explain this?
It seems that when execution of program comes to %d in printf function, it immediately substitutes this with a value similar like a = b++ no matter there are parenthesis.
Regards
-
Those parentheses do not matter here since there is only one way to group the single increment/decrement expressions.
-
Well parentheses have no effect on when side effects happen.
Or if there is an effect, it's entirely arbitrary.
The only thing you know is that all side effects have happened by the time the next sequence point is reached.