I have encountered a sorcery today.
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 of5
.i++
returns5
.i
is post incremented byi++
(to6
).i
gets the value of5
(returned byi++
).- 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.