Can anyone please explain me the following results?
int y,x;
x=4;
y=x+ ++x + ++x;
printf("%d",y);
The output comes out to be 16. How?
Can anyone please explain me the following results?
int y,x;
x=4;
y=x+ ++x + ++x;
printf("%d",y);
The output comes out to be 16. How?
I'm pretty sure that this sort of behaviour is undefined by the C standard, and so your compiler can make the answer anything it well pleases.
Don't use use the increment operator on the same variable twice in the same expression.
Pretty straight forward, the increment has precedence (both of them), so it's 6 + 4 + 6.
All these ++--++-- stuff, when they have the same sequence point, are baloney, compiler specific, and probably undefined, as well.
Don't use them on the same variable, in the same line of code.
Google keyword: sequence point.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.