GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

help me with the basic yet tricky concept. ASAP thankyou so much in advance.

#include<stdio.h>
int main()
{
int a=10;

printf("%d %d %d ",a++,a,++a);
return 0;
}

 

asked Aug 21, 2016 by aravindsampath

1 Answer

The compiler will evaluate printf's arguments in whatever order it happens to feel like at the time. It could be an optimization thing, but there's no guarantee: the order they are evaluated isn't specified by the standard, nor is it implementation defined. There's no way of knowing.

But what is specified by the standard, is that modifying the same variable twice in one operation is undefined behavior.

printf("%d %d %d\n",a++, a,++a); could do a number of things; work how you expected it, work in ways you could never understand.

NOTE:- I have read that evaluation is done right to left in printf. But,The order is unspecified,  modifying the same lvalue twice in the same expression (without a sequence point in between) is actually undefined behavior, and can thus do anything at all - including crash.

 

"I tried my best to explain it. I apologize if it's not clear to you but do let me know if you have any confusion." Thank You . "KEEP CODING" :-)

answered Aug 21, 2016 by Mazhar MIK

hey! that's a very nice explanation. thankyou. i was in a dilemma that,it would be having no significant rule or so, and u just made it clear. 

it was soo helpful and thanks to clear my doubt.

I am glad my friend. Keep Coding. :-)

...