Can anyone explain it to me that why we are getting 18 instead of 17 | SoloLearn: Learn to code for FREE!

0

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

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

1/21/2021 7:34:35 AM

Aman Sharma

10 Answers

New Answer

+4

Same problem answered in this thread. https://www.sololearn.com/Discuss/2662995/?ref=app

+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.

+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

+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

+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)

+1

《 Nicko12 》 No bro I am not talking about that I am asking why the output Is 18 instead of 17.

+1

Aman Sharma kindly read my answer and the first thread shared by 《 Nicko12 》 because there is no definite answer to this.

+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.

0

Woh

0

RKK I didn't get the last part.