+ 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; }
61 Respostas
+ 3
Nice code
but little bit problems
+ 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
+ 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.
+ 2
Tushar Kumar So your question is not about why i is not equal to 4?
+ 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.
+ 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.
+ 1
Neha Kumari ???????
+ 1
Neha Kumari Yes Sure, How can I help you?
+ 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.
+ 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.
+ 1
Eashan Morajkar Yes, I'm expecting i to get 4 in next line but it never happens.
+ 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
+ 1
But what about the warning
+ 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,
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?
0
Calvin Thomas Yes that's what I was thinking also.. also some compiler is giving output as 3 :/
https://onlinegdb.com/VHgvctslg
0
Eashan Morajkar You mean a variable will never increment if same variable name is used for assignment?
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++;"
0
Eashan Morajkar Exactly it's kind of creating ambiguity
0
What is kroge?