how does post increment works in c? [duplicate] - c

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
here is very simple c program:
int main()
{
int i = 22;
printf("%d %d %d",i,i++,i);
return 0;
}
The result is:
23 22 23
how exactly post increment is working here ?

You cannot test correctly in this context.
The order of parameter evaluation for function parameters is undefined, hence you will have platform-dependent results.
The way i++ works actually is by first returning the old value and later incrementing, but that is a bad test.

Judging by the result you got, i++ is evaluated and returns the pre- incremented value. Then, the value of i, the return value of i++, and the value of i are passed to the print function.
This isn't ever something that you should rely on, as you may get different answers on different compilers or even with different optimization settings. As in the other answer, the order of parameter evaluation is undefined behavior.

Related

Does scanf function cause any logical problems with operators? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 4 years ago.
I think the best way to describe the problem is to actually show the output of this simple code:
Image contains code and output
#include<stdio.h>
int main()
{
int a=5,b;
b=a-++a;
printf("%d %d",a,b);
return 0;
}
As you can see, the values returned are logically correct.
++a increases the value of a to 6. Then b=6-6=0.
However, when I take the value of 'a' as user input using scanf, the following happens:
Image contains code and output
#include<stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
b=a-++a;
printf("%d %d",a,b);
return 0;
}
Shouldn't the results be identical or am I missing something simple here?
The form a - ++a has an undefined value because the order of executions of the argument of '-' is undefined.
In the first case the compiler knows the value of a so it can optimize the code and finally all is known at compile time, not in the second case.
The fact the value is not the same when computed at compile time or execution time is a consequence of the form a - ++a whose result is undefined.

how is this post increment working? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
#include <stdio.h>
int main ()
{
int a=10;
printf("%d %d %d",a,a++,a);
return 0;
}
The output I am getting is "11 10 11".
I thought the output would be "10 10 11".
why a is incrementing like this?
Because there is no guarantee about the order in which a C compiler evaluates the arguments. The only thing guaranteed (by the standard) is that they are all evaluated before doing the call. Therefore, you should never count on the order of evaluation of the arguments. Just consider it as random.
Hence, in general, avoid using auto-increment if the same variable exists more than once in an argument list.

Getting strange result while using increment operator(++) in printf fuction [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
I am executing this c program on gcc compiler and getting strange results.
So how is it possible
code:
#include<stdio.h>
int main()
{
int i;
i =10;
printf(" %d %d %d ",i++,i++,i); //output : 11 10 12
return 0;
}
as per me result should be 10 11 12 but I am getting 11 10 12.
How is it possible?
In C++, the order of evaluation of function arguments is undefined so if you use the increment operator multiple times in the argument to a particular function there is no 'correct' answer, they may be evaluated in any arbitrary order.
Please familiarize yourself with the concept of Sequence points. Only at such defined sequence points is guaranteed that all side effects of previous evaluations are performed. There are no sequence points between the list of arguments of a function. So, it leads to undefined behavior.

Why does i=2+2*i++ give the wrong result? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I felt that such an expression should be invalid but I was able to compile it and got the answer 5.
At the end I felt that even if it does answer should be 4 not 5.
int main(void)
{
int i=1;
// how is the next line evaluated ie in what sequence??
i=2+2*i++;
printf("%d",i);
return 0;
}
The output I got was 5. I can not understand how it should give the value.
This is undefined behaviour, since i is modified more than once between sequence points. For instance, this compiler gives 4 as the answer, because it puts the increment after the assignment. Another reasonable answer is 6, if the increment is before the assignment. But, as you've found, the compiler is permitted to make the answer whatever it wants, including 5.
See here for more about sequence points and undefined behaviour.

Yet an other i = i++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I have encountered a sorcery today.
http://codepad.org/VW2vTpWw
Language: C
Code:
#include <stdio.h>
main()
{
int i = 5;
i = i++;
printf ("%i", i);
}
Output:
6
How? Why?
This is supposed to be tricky code but the other way around. The negligent programmer would think that i = i++ is just simple increment but it's not. Yet it works like one here. It's supposed to be 5! Like in JavaScript.
What should be happening.
i gets the value of 5.
i++ returns 5.
i is post incremented by i++ (to 6).
i gets the value of 5 (returned by i++).
the value of i (5) gets printed.
Yet it is 6.
I haven't been able to find a description to this on SO, or the whole internet (just the other way around).
What is broken here?
Please explain.
It is undefined behavior to store more than once to an object without an intervening sequence point.
In particular, your step 3 and 4 have no defined ordering, the increment (and store) or the store could happen first.

Resources