onkar said:
I was expecting 4 5 5 4 5
From your reply, I deduce that you were expecting the compiler to evaluate
the printf arguments from right to left. But in fact C doesn't guarantee
any particular evaluation order for function arguments. To get the output
you require, do this:
#include <stdio.h>
int main(void)
{
int i = 5;
int z = i;
int y = --i;
int x = ++i;
int w = i--;
int v = i++;
printf("%d %d %d %d %d\n", v, w, x, y, z);
return 0;
}
This guarantees the output 4 5 5 4 5
It is always a bad idea to make a line of C code do too much. Break complex
expressions down into stuff you can actually understand. (If you thought
you understood printf("%d %d %d %d %d\n",i++,i--,++i,--i,i), it should be
evident from your surprise at the output that you were mistaken to think
that.)
In general, if you're using ++ or -- on any particular object (or indeed
modifying the object's value in any way), that should be the only place
you use that object within that whole statement. Otherwise, the behaviour
of the code is not defined by the language spec.