+ 2
Unexpected behavior of increment operator
Can anyone explain why value of i is not increasing in this code?? #include <stdio.h> int main() { int i=0; i=i++; i=i++; printf ("%d",i); printf ("%d",i); return 0; } //output : 00
15 ответов
+ 2
Akshay Jain I know. Maybe my first answer wasn't clear enough though. There's no doubt that a++ returns a and then sets a to a+1. If you now execute a = a++, it would actually do something like this:
a = a;
a = a + 1;
As it turns out the second line is executed first and then the first line is executed. Therefore the program sets a to a+1 but then executes the first line which sets a back to its previous value. A equivalent piece of C code would look like this:
a_ret = a;
a = a + 1;
a = a_ret;
The magic here is the return value that a is assigned to as last operation: the return value of a++ (a_ret) is stored separately from a and therefore isn't affected by a = a+1.
+ 7
Because first you are assigning the value of i before incrementing to i so next time i will be 0.
Again you are assigning with 0 value. That's why i is 0.
+ 5
i++ returns i and then increases it,
++i increases i and the returns i.
i=i++ does nothing: the i++ statement returns i and i is increased but then i set to to returned value which is the previous i.
+ 5
Akshay Jain As per your example b is different memory allocation but as per your question i is in same memory allocation so i will not increment it will be remain same means 0. Try to print the value of i after i = i++
+ 3
Akshay Jain It's neither rule nor exception. It's a working behavior. You need to learn about memory allocation.
+ 3
changing a variable twice without an intervening sequence point is one example of undefined behaviour.
the "i=i++;" expression causes an undefined behavior since this expression does not contain any sequence points inside.
GCC: https://godbolt.org/z/sx4aUB
Clang: https://godbolt.org/z/hi66tM
sequence points: https://en.wikipedia.org/wiki/Sequence_point
+ 2
Try ++i instead
+ 2
Gevork Bagratyan I know that that's why I'm asking why this code is not working..Thanks for answering
+ 2
We may deduce that at the machine instruction level i=i+1 resulted in the following steps:
1. Load i from memory into a CPU register.
2. Increment memory location i.
3. Store the register into memory location i.
That explains it. Furthermore, because the net effect is no change to i, there is a strong possibility that these instructions even got eliminated by the optimizer at compile time.
+ 1
A also want to add that this discussion is basically meaningless for actual coding. There's a good reason why the ++ operator should be used carefully and isn't even part of the Rust language: It creates many implications that are hard to read and understand just like the code in your question. Writing C code that rarely uses the ++ operator is - in my opinion - a good practice.
0
🅰🅹 - Sʀ. Sᴏғᴛᴡᴀʀᴇ Eɴɢɪɴᴇᴇʀ here shouldn't the value of i increase after it is first assigned to i.And then again when it is assigned to i it should assign the added value,so it should be 01.I know i am wrong coz i tested it but please explain why.(Edit-The output will be 22 with this logic,i saw from the answer below).
0
🅰🅹 - Sʀ. Sᴏғᴛᴡᴀʀᴇ Eɴɢɪɴᴇᴇʀ As I'm assigning the value first it assigns 0 but after first line it should increment and then in second line, firstly it'll assign 1 and then increment should be done.. so the output should be 22
0
Aaron Eberhardt if a=b++ then a is assigned first with b then b is increased. Similarly if i=i++ is there i should be assigned 0 first and then increase by 1
0
🅰🅹 - Sʀ. Sᴏғᴛᴡᴀʀᴇ Eɴɢɪɴᴇᴇʀ so it's a rule or exception like something.. right?
0
detail information for C# here, but also the same as what C and Cpp do:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#prefix-increment-and-decrement-operators
> The result of the operation is a value of the same type as the operand.
and note that 'int' is immutable.
So in fact this is what your code really does:
int i1=0;
int i2=i1;
i1++;
int i3=i2;
i2++;
printf ("%d",i3);
printf ("%d",i3);
you named 'i1', 'i2', 'i3' the same name 'i', but in memery they are at different addresses and have their own space.
\