pre and post increment operations on a variable give different output on TC and gcc [duplicate]

StackOverflow https://stackoverflow.com/questions/8681881

  •  10-05-2021
  •  | 
  •  

Question

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 ???

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top