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.
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 1 year ago.
I'm not able to understand, why this code produces this output. Can anyone help me with this??
#include <stdio.h>
int main( void )
{
int num = 5;
printf( "%d %d %d", num, ++num, num++ );
}
The output will be 7 7 5 (and NOT 5 6 7)
Making side effects happen in the same order as in some other compiler.
It is never safe to depend on the order of evaluation of side effects. For example, a function call like this may very well behave differently from one compiler to another:
void func (int, int);
int i = 2;
func (i++, i++);
There is no guarantee (in either the C or the C++ standard language definitions) that the increments will be evaluated in any particular order. Either increment might happen first. func might get the arguments ‘2, 3’, or it might get ‘3, 2’, or even ‘2, 2’.
https://gcc.gnu.org/onlinedocs/gcc/Non-bugs.html
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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
There is a code following below and i ma facing a very serious problem in understanding the logic for the code.
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
int i = 1 ;
printf("\n%d %d %d %d\n",++i,i++,i++,++i) ;
return 0 ;
}
I am using gcc compiler under the linux distro named Mandriva. In the above mentioned i have used pre and post increment with a variable in the printf statement.
The output that i am supposed to get is 2 2 3 5, but i am getting a different output.
Please help me in this code.
I am feeling much difficult in this code.
It's undefined behavior. There's no sequence points between the increments of i.
Any result is a correct result (including your hard drive being formatted).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
I have this code but i'm not getting how its functioning.
main()
{
int i=1;
i= ++i + ++i + ++i;
printf("%d",i);
}
I tried to compile it and im getting the output 10 but i've no idea how.
What I figured out is the two ++i are getting assigned the value 3 and the one ++i is getting the value 4, before the addition operation is performed. I cant figure out how increment operator is working in this case. Plz help me out with this.
The behavior is undefined .. there are a lot of posts similar to this if you search on SO.
For instance What would the evaluation order of x = x++ + ++x; be? or Why are these constructs (using ++) undefined behavior? and more.
Finally, just an opinion/comment: I don't think anyone would advocate writing that type of code as it's also hard to understand (hence the reason for your question).
I'm pretty sure you're not supposed to do that. Basically, don't modify a value more than once inside of the same expression. To do otherwise invokes "undefined behavior", which is a fancy way of saying "the compiler makes no guarantees of what will happen.
(Technically, the rule is don't modify a value more than once between the same sequence points)