+ 5

Quiz , Why output is 3!

Help me! I found this question from a website (https://www.indiabix.com/c-programming/expressions/014002) and it showing it's output as 4. And I expecting the same output but in many compilers output of it is showing as 3. Also sololearn compiler is not able to compile this code, throwing this error. "warning: operation on 'i' may be undefined [-Wsequence-point] " https://code.sololearn.com/cA21a25A1A15/#c #include<stdio.h> int main() { int i=3; i = i++; printf("%d\n", i); return 0; }

7th Jun 2021, 11:05 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
61 Respuestas
+ 3
Nice code but little bit problems
9th Jun 2021, 4:40 AM
Tharul Nejana
Tharul Nejana - avatar
+ 2
Youre using postfix incrementation which means first it will assign I the value of 3 and then be 4 if you had used ++i then it would have been 4
7th Jun 2021, 11:16 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 2
Well, I guess that it isn't undefined or ambiguous. Consider this code snippet: ... int k = 56; int a = k++; vs k = k++; Here, the equals operator has a lower precedence than the post increment operator, right? Then why is it undefined? I'm confused.
8th Jun 2021, 6:08 AM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Tushar Kumar So your question is not about why i is not equal to 4?
8th Jun 2021, 6:09 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Eashan Morajkar Wouldn't the variable be incremented after the assignment? I guess that this is just an undefined behavior which the compiler can't execute properly.
7th Jun 2021, 12:41 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Hey guys! just found this maybe it opens up another perspective https://stackoverflow.com/questions/4968854/is-the-behaviour-of-i-i-really-undefined I remember someone posted same question before time, but unlucky me I can't find it, maybe it was deleted, poorly tagged or titled Idk.
7th Jun 2021, 12:58 PM
Ipang
+ 1
Neha Kumari ???????
7th Jun 2021, 3:57 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Neha Kumari Yes Sure, How can I help you?
7th Jun 2021, 4:09 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Abhishek Kumar Nah I was actually solving quizes I found this one. Pre increment gonna work just wonding why post incremental is not working as after assignment, when printing it should increment by 1 and should print 4.
8th Jun 2021, 3:38 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Calvin Thomas Well that's what I was wondering even if some compiler is able to comple it never get incremented to 4 it just stay 3 no matter how many time I print i but according to postfix rule it should get incremented to 4 in next line. Also a point to be noted that this happen only when assigning i++ to itself i but insted of i when assigned to another variable like j it works alright and will get incremented to next line and will output 4.
8th Jun 2021, 7:25 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Eashan Morajkar Yes, I'm expecting i to get 4 in next line but it never happens.
8th Jun 2021, 7:27 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
He could have also done this i=++i; But he doesn't want that, he wants to find why is it not going to 4 when it should have
8th Jun 2021, 8:37 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
But what about the warning
8th Jun 2021, 9:32 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
If I'm right I come to a conclusion that here the same value is being modified twice which is causing abominations like undefined behavior. Maybe, Because post-increment operator first increments the variable and stores its old value reference after assignment it returns the old value. So first i will increment the variable and then return the old value which again getting assigned on i and update the i value form 4 to 3 back again,
8th Jun 2021, 12:58 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Eashan Morajkar But as i++ is post increment it will first assign 3 to i then value of i will increment to 4. So when it will print it should print 4 right?
7th Jun 2021, 11:23 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Calvin Thomas Yes that's what I was thinking also.. also some compiler is giving output as 3 :/ https://onlinegdb.com/VHgvctslg
7th Jun 2021, 11:25 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Eashan Morajkar You mean a variable will never increment if same variable name is used for assignment?
7th Jun 2021, 11:45 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Calvin Thomas yup it is, First i tried it on my gui c compiler, it gave no error, then i tried it on the terminal gcc compiler, again no error but then i tried it on my clang compiler it did not raise and error but an warning "warning: multiple unsequenced modifications to 'i' [-Wunsequenced] i=i++;"
7th Jun 2021, 12:54 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
Eashan Morajkar Exactly it's kind of creating ambiguity
7th Jun 2021, 4:01 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
What is kroge?
7th Jun 2021, 4:02 PM
Eashan Morajkar
Eashan Morajkar - avatar