+ 2

🛑Urgent🛑: Can anyone explain me that how compiler execute this? [Read Description] 🥺🥺 Please help! 😔

#include<stdio.h> int main() { int i = 1; int j = i++ + (i = i + 5) + i++ + i; printf("%d", j); return 0; } Output: 23 Why this output?

21st Feb 2021, 9:19 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
26 Respuestas
+ 5
It produce a error.becuse you can not intilize variable more than one time. And output 23 Here is answer remember i=1 j=i++ +(i=i+5)+ i++ +i; 2+(2+5)+7+7; 2+7+7+7; 21+2; 23.
23rd Feb 2021, 7:03 AM
gajanand dave
+ 4
Angelo Consider i=1 j = (i=i+2) + i++ + i; What will be the j's value? Here, 3 + 3 + 4 Then after this, Why compiler updates the first term again? 4 + 3 + 4 = 11 Output is 11.
21st Feb 2021, 9:40 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 4
I++=1;// i become 2 (increment) i=2+5; //now I assign 7 I++=7;// now I assign 8 I=8; // final output by adding all that is 23;
22nd Feb 2021, 3:50 AM
NITIN
NITIN - avatar
+ 3
OMG Undefined behavior, Undefined behavior everywhere! It is doing: i=1 +1 i=2 (i=2+5)—>+7 i=7 +7 i=8 +8
21st Feb 2021, 9:28 AM
Angelo
Angelo - avatar
+ 3
Angelo #include<stdio.h> int main() { int i = 1; int a = i++ + (i = i + 5) + (i = i + 5); printf("%d", a); return 0; } Output: 20 Here, Why second term was not updated again after third term? I was expecting 25. I am just confused.
21st Feb 2021, 9:56 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 3
Angelo #include<stdio.h> int main() { int i = 1; int a = (i = i + 2) + i++ + i; printf("%d", a); return 0; } Why output 11?
21st Feb 2021, 10:05 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 2
It's normal that you are confused, we don't really know what the compiler is doing... That's the meaning of undefined behavior
21st Feb 2021, 9:58 AM
Angelo
Angelo - avatar
+ 2
Ok, I think it is doing some think like: extract all the i++ substitute them with i-n So: i++ + (i=i+5) + i++ + i becomes i++; i++ —> i=3 (i-1) + (i=i+5)+(i-2)+i —>23 (i=i+2) + i++ + i Becomes i++ —> i=2 (i=i+2)+(i-1)+i —>11 i++ +(i=i+5)+(i=i+5) Becomes i++ —> i=2 (i-1)+(i=i+5)+(i=i+5) —>20 As you see I can only make hypothesis that are valid as long as you find an exemple that breaks it (just like with science)
21st Feb 2021, 10:53 AM
Angelo
Angelo - avatar
+ 2
NITIN According to your logic, #include<stdio.h> int main() { int i = 1; int a = (i = i + 2) + i++ + i; printf("%d", a); return 0; } This program's outout should be 10. But, why output is 11?
22nd Feb 2021, 10:44 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 2
Martin Taylor I'm not sure, C/C++ standard should have a precise operator precedence table, so the result should be unic. Mahima Rajvir Singh this is the operations' order: First of all, i = 1 j = i++ so (j = 1) then (i = 2) j += (i = i + 5) so (i = 7) then (j = 8) j += i++ so (j = 15) then (i = 8) j += i so (j = 23) and still (i = 8) Hope I've been clear.
22nd Feb 2021, 5:18 PM
OrHy3
OrHy3 - avatar
+ 2
Sébastien Ranganayaguy , OrHy3 & Dinesh Rathod 👇🏻 According to your logic, #include<stdio.h> int main() { int i = 1; int a = (i = i + 2) + i++ + i; printf("%d", a); return 0; } This program's outout should be 10. But, why output is 11?
23rd Feb 2021, 5:43 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 2
OrHy3 So why it isn't the same in this program? This is what you have explained. First of all, i = 1 j = i++ so (j = 1) then (i = 2) j += (i = i + 5) so (i = 7) then (j = 8) j += i++ so (j = 15) then (i = 8) j += i so (j = 23) and still (i = 8) Thanks for listening my doubts! 😊
24th Feb 2021, 5:18 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 1
Btw, I see you're experimenting a lot with the language That's great, keep it up!
21st Feb 2021, 9:33 AM
Angelo
Angelo - avatar
+ 1
Output 20 can be: i=1 +1 i=2 +7 i=7 +12 i=12
21st Feb 2021, 10:03 AM
Angelo
Angelo - avatar
+ 1
J=1+7+7+8;
22nd Feb 2021, 3:45 AM
NITIN
NITIN - avatar
+ 1
I try that code in java and the output is 10 I don't now about 11;
22nd Feb 2021, 11:32 AM
NITIN
NITIN - avatar
+ 1
And I am not sure but it's because of Operators Precedence I think you need to read that out then you will understand
22nd Feb 2021, 11:49 AM
NITIN
NITIN - avatar
+ 1
NITIN you are a genius!!! We are trying to understand how C++ works and you talk about Java! GENIUS!
22nd Feb 2021, 12:49 PM
Angelo
Angelo - avatar
+ 1
I think : when u use i++ the value of i will change for the next time u call i. i++ + (i = i + 5) + i++ + i; So at the first i++, i = 1 But at i = i + 5, i = 2 for the calcul. At the seond i++, i = 7, Then the last part + i, i = 8. Finally u have : j = 1 + (2 + 5) + 7 + 8 = 23.
22nd Feb 2021, 4:22 PM
RPSebb
RPSebb - avatar