Unexpected behavior of increment operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th Jan 2020, 6:45 PM
Akshay Jain
Akshay Jain - avatar
15 Answers
+ 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.
28th Jan 2020, 7:56 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 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.
28th Jan 2020, 6:49 PM
A͢J
A͢J - avatar
+ 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.
28th Jan 2020, 6:51 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 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++
28th Jan 2020, 7:21 PM
A͢J
A͢J - avatar
+ 3
Akshay Jain It's neither rule nor exception. It's a working behavior. You need to learn about memory allocation.
28th Jan 2020, 7:41 PM
A͢J
A͢J - avatar
+ 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
28th Jan 2020, 8:44 PM
MO ELomari
+ 2
Try ++i instead
28th Jan 2020, 6:47 PM
Gevork Bagratyan
+ 2
Gevork Bagratyan I know that that's why I'm asking why this code is not working..Thanks for answering
28th Jan 2020, 6:49 PM
Akshay Jain
Akshay Jain - avatar
+ 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.
28th Jan 2020, 10:40 PM
Brian
Brian - avatar
+ 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.
28th Jan 2020, 8:01 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
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).
28th Jan 2020, 6:52 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
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
28th Jan 2020, 6:53 PM
Akshay Jain
Akshay Jain - avatar
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
28th Jan 2020, 7:07 PM
Akshay Jain
Akshay Jain - avatar
0
🅰🅹 - Sʀ. Sᴏғᴛᴡᴀʀᴇ Eɴɢɪɴᴇᴇʀ so it's a rule or exception like something.. right?
28th Jan 2020, 7:29 PM
Akshay Jain
Akshay Jain - avatar
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. \
29th Jan 2020, 11:46 PM
o.gak
o.gak - avatar