3

Here is my simple code ...

#include<stdio.h>
int main()
{
int i=5;
printf("%d %d %d %d %d ",i++,i--,++i,--i,i);
return 0;
}

On gcc,it gives output as '4 5 5 5 5'

but on TC,it gives output as '4 5 5 4 5'

what I know that in printf statement,evaluation will be from left to right if it is a single expression but in normal statement,it will be from left to right.

but if printf contain multiple expressions,then evaluation will be on stack,the elements would be pushed onto stack from left to right but popped out from right to left and that justified the TC output

Please correct me where am I wrong ???

1
  • 1
    @JensGustedt: It's not really a duplicate of that question, which doesn't have undefined behavior (though the accepted answer does cover that issue). It should be marked as a duplicate of one of the plethora of other questions about abuse of ++. Dec 30, 2011 at 17:50

5 Answers 5

8

C does not specify which order function arguments should be evaluated in, and so it is undefined and a compiler can do it however they choose, including arbitrarily and randomly. Bjarne Stroustrup says this explicitly in "The C++ Programming Language" 3rd edition section 6.2.2

He also gives a reason:

Better code can be generated in the absence of restrictions on expression evaluation order
2
  • +1 Very good to see the reference that backs up the statement. Dec 30, 2011 at 16:58
  • 3
    @PeteWilson The reference is to a C++ book in an answer to a C question. The answer uses "undefined" where it should use "unspecified" (the order of evaluation of function arguments is unspecified. These two words do not mean the same thing). And that's the wrong explanation anyway, since the OP's program really invokes undefined behavior by failing to separate assignments to i with sequence points. Dec 30, 2011 at 17:15
3

I think the order in which the arguments of a function call are evaluated is not specified. As wikipedia says in this article on sequence points:

The order in which the arguments are evaluated is not specified

1
  • Yes, but that's not the whole story. The behavior isn't limited to evaluating the arguments in arbitrary orders; it's truly undefined. The output could be 42, or "a suffusion or yellow", or a suffusion of yellow. Dec 30, 2011 at 17:47
3

Modifying an object (in this code i) more than one time between the previous and the next sequence point is undefined behavior in C. Here the sequence point occurs at the function call after all arguments have been evaluated.

0

The two answers at this point in time invoke the unspecifiedness of the evaluation of function arguments. The correct answer is that your program is undefined because of side-effects to the same variable not separated by a sequence point.

Indeed, the evaluation order of function arguments is unspecified. This means that in the statement f(g(), h());, either g() is called before h() or it is called after.

However, unsequenced side-effects (as in your program) invoke undefined behavior, where anything can happen.

0

Bumping up an old topic but I just found how the gcc and Visual Studio Compiler works on multiple changes on the same variable in a statement so thought of sharing it here.

The compiler as defined here starts to implement stack method on the arguments being passed in printf which is 'i'. It follows these rules:-

1) Now it executes the pre-increments first therefore starting from right normal i then --i and then ++i are executed and the value of i after ++i is 5 so it implements these values (pops) so output is _ _ 5 5 5

2) Then it continues right to left and executes post increments therefore, i-- and then i++ so the value of i in the end is back to 5 but due to i-- it becomes 4 but shows 5 due to it being a post increment therefore the final output is 4 5 5 5 5 and the final value of i is 5

Hope I am able to clear your doubts.

TC doesn't follow this so it adheres to our human logic.

Not the answer you're looking for? Browse other questions tagged or ask your own question.