Hi
Can any one help me what is the order of evaluation for the expressions in printf() function in C language.....

I have sooo much confusion in the expression like

a=10;
printf("%d",a++ - a++);

the output is -1

any one can help me how -1 is the output......
But when i am assiging the result of that expressiion in any variable then i got 0 value...I don't know the difference..

Thank You

When you realise all such expressions are undefined, you can relax and get some sleep.
http://c-faq.com/expr/index.html

> But when i am assiging the result of that expressiion in any variable then i got 0 value
That's one of the hallmarks of undefined behaviour, change anything at all and the result changes for no good reason.

If someone is teaching you this stuff, they're an idiot.

As far as I know, that code is ambiguos and you should avoid such expressions try using ( a++ ) - ( a++ )

but the statement goes from left to right

a++ = 11 MINUS a ( which is now 11) ++ which makes it 12.

So the statement reads 11 - 12 = -1

That is correct.

btw the same code on my compiler is 0 not -1

Did you even bother to read the link I posted?
Parentheses ONLY modify precedence, they do nothing to evaluation order.

printf("%d",a++ - a++);

Undefined Behavior. Meaning, is legal C syntax but is outside of any established agreed process
defined in the C standards. Therefore, the compiler may or may not interpret or process the given statement in the correct intent.

Which rule is a++ - a++ not following?
Well, it has to do with sequence points. A fancy phrase for "from this part to this part you can only do these things" at which end is guaranteed that everything that it needs to be done ( side effects )is completed.
Increment and decrement operations will be only perform after the end of a sequence point and
before another. a - a it would be an expression evaluation, there's a sequence point at the end of every evaluation but not in between, therefore the increment of the first a++ is unexpected and the second it would only happen afterwards; moreover the order of precedence is nullified.

> Increment and decrement operations will be only perform after the end of a sequence point
No, they happen at any point between sequence points (therein lies part of the problem).
http://c-faq.com/expr/evalorder2.html
http://c-faq.com/expr/seqpoints.html

> therefore the increment of the first a++ is unexpected an the second it would only happen afterwards;
Again, this is also misleading.
Given a = f() + g(); there is nothing in the standard which says that f() MUST be called first. So you can't be sure that the left hand side value (with its side-effect) is "evaluated" first.

> Increment and decrement operations will be only perform after the end of a sequence point
No, they happen at any point between sequence points (therein lies part of the problem).

Thank you for clarifying that. I didn't do a good job explaining what I meant. Yes it happens at any time and that's why is undefined in that situation. What I meant it that is ONLY guarantee to produce the result that you intent if you pay consideration to the sequence point and side effect is completed.

> therefore the increment of the first a++ is unexpected an the second it would only happen afterwards;
Again, this is also misleading.
Given a = f() + g(); there is nothing in the standard which says that f() MUST be called first. So you can't be sure that the left hand side value (with its side-effect) is "evaluated" first.

I can see again where my comment could be misleading too. Once again you clarified the nature of why is undefined in first place. That part of my comment made more sense in my mind from the point of view of one sequence point completed and before another one.
Well, I tried to give it a stab at it. It's fine to say "Undefined Behavior" and be done with it, assuming that the reader will know what the heck it is; however I thought of giving away a little more explanation.
I will not guarantee that next time I will leave it to the experts


Btw for any one that would like to know what would constituted a sequence point, here it is:

* at the completion of evaluation of each full-expression;
* after the evaluation of all function arguments (if any) and before execution of any expressions or statements in the function body;
* after the copying of a returned value and before the execution of any expressions outside the function;
* after the evaluation of the first operand of the following operators: && (logical AND); || (logical OR); ? (conditional)
* after the initialization of each base and member in a class.

Did you even bother to read the link I posted?
Parentheses ONLY modify precedence, they do nothing to evaluation order.

Yes, I already understand what you are saying, guess you failed to read my post. The code is ambiguous and parenths would be even a futile attempt and about the only chance there would be to make sense of such code. However, C manuals clearly state such code produces undefined behavior.

Simple conclusion, again,

that code is ambiguos and you should avoid such expressions

Of course I read your post, in particular where you attempted to "justify" an interpretation.

but the statement goes from left to right

a++ = 11 MINUS a ( which is now 11) ++ which makes it 12.

So the statement reads 11 - 12 = -1

That is correct.

Is that what you determined from observations of how your compiler behaves?

But it can't be, because you go onto say
> btw the same code on my compiler is 0 not -1

The only good thing about your post was the first line.

This article has been dead for over six months. Start a new discussion instead.