0

Can anyone explain it to me that why we are getting 18 instead of 17

https://code.sololearn.com/c3huj9apOlb5/?ref=app

21st Jan 2021, 7:34 AM
Aman Sharma
Aman Sharma - avatar
10 ответов
+ 4
Same problem answered in this thread. https://www.sololearn.com/Discuss/2662995/?ref=app
21st Jan 2021, 8:04 AM
noteve
noteve - avatar
+ 4
The warning pretty much clearly informs you that there is a sequence point issue. You are trying to modify a variable more than once in a single expression and this behaviour is not so well defined in the C or C++ language. The output may differ for different compilers.
21st Jan 2021, 8:07 AM
Avinesh
Avinesh - avatar
+ 2
[Explanation for the Aman Sharma's code after line 6 ] The following is my what I think: 1) We can get the hint from the definition of prefix increment("The prefix increment returns the value of a variable after it has been incremented.") that is first pre-increment both the variable a and then return the value of it. like so, i = 2 In our case, a = ++i + ++i; Initial Value = 2 First Prefix = 3 Second Prefix = 4 Final Equation a = 4 + 4 = 8 So, it's 18. 2) Another reason maybe because of where and how C/C++ compiler stores variable during the execution. (I play a bit with the code and got this amazing result, I am learning as well so I am confused too) https://code.sololearn.com/czGw8Vrb3Sxd/?ref=app
21st Jan 2021, 8:37 AM
minirkk
minirkk - avatar
+ 2
Aman Sharma The reason why it outputs 18 is not because of the incremention But because you did not print a new line. The first prints 1 and the second prints 8. And because it does no have new line, it will output 18. When kt should have been 1 and 8. The output should have look like this: 1 8 The first one is because you used suffix incremention. i++ + i++ Use the variable( 0 ) + Then used again th inremented variable ( 1 ) 0 + 1 == 1 But in the next lines of the program, the value of i will become 2 becausw we have 2 incrementions. The second one is said by RKK and the thread I shared. https://code.sololearn.com/cc9rT05E4551/?ref=app
21st Jan 2021, 9:06 AM
noteve
noteve - avatar
+ 1
[Explanation for the Aman Sharma's code after line 6 ] MEMORY i [ 2 ] a = ++i + ++i; We know the associativity of prefix operation is from Right to Left( R ---> L). So ++i(the right one) gets executed first which will make the initial value of i from 2 to 3 and we have MEMORY i [ 3 ] Again, ++i(the left one) gets executed which will make the value of i in memory incremented(from 3 to 4) and then update, we have MEMORY i [ 4 ] So the most updated value of i in the memory is 4 so we can relate from the definition of prefix-increment( "The prefix increment returns the value of a variable after it has been incremented.") that the updated incremented value be returned that is i = 4 So our equation becomes a = 4 + 4 = 8 which is 8. (correct me if I am wrong here)
21st Jan 2021, 8:49 AM
minirkk
minirkk - avatar
+ 1
《 Nicko12 》 No bro I am not talking about that I am asking why the output Is 18 instead of 17.
21st Jan 2021, 9:08 AM
Aman Sharma
Aman Sharma - avatar
+ 1
Aman Sharma kindly read my answer and the first thread shared by 《 Nicko12 》 because there is no definite answer to this.
21st Jan 2021, 9:10 AM
Avinesh
Avinesh - avatar
+ 1
Aman Sharma which part? You need to dry run yourself too. Try to execute yourself with the help of copy and pencil, you'll understand well.
21st Jan 2021, 9:22 AM
minirkk
minirkk - avatar
0
Woh
21st Jan 2021, 8:05 AM
Abhay
Abhay - avatar
0
RKK I didn't get the last part.
21st Jan 2021, 9:02 AM
Aman Sharma
Aman Sharma - avatar