0
Why the out put of this code is 71?
How is the calculation of (-a+b*a++) in line 9? https://code.sololearn.com/c2Q79dxTNCk2/?ref=app
13 Answers
+ 1
In c/c++, operator precidence tells which one operator sholud bound to which expression by precidence of operator but still compilers free to return results by their implementation specific. In other languages like java there is no sequence point issue because of strict calculations..
This will explain to you better I think...
https://stackoverflow.com/questions/44770170/what-is-the-difference-between-a-sequence-point-and-operator-precedence
Edit:
https://stackoverflow.com/questions/3575350/sequence-points-in-c
+ 3
Read the warnings.. It has sequence point issue. Means it is undefined by standards and output may vary depending on the compilers. In some compilers, is 72, in some compilers 71
It may calculated a++ first, or b*a, or in order -a, b*a, a++. So output is compiler specific....
https://en.m.wikipedia.org/wiki/Sequence_point
+ 2
what was your desired output?
+ 2
This code works! well
If we say (-6+13*6) the answer is 72.
But it seems that the software says: (13*6)
Then a++=7
And last - 7 + 78 =71
+ 2
Ehsan ghazanfary
This is excuted as right to left cause of binary precedence.
See:
a++ means read the initial value then increase by one which results in following statement =
(-7+13*6)=71
Use BODMASS
To double check this now if u check following statement the answer will be same cause of reason I explained:
(-a++ b*a++) =71
+ 1
Due to binary precedence. U have to take care of precidence operator
0
you should be using a + 1
0
72
0
check my code👆
0
Yeah I saw the warnings but I calculated this code using standard table of C operaters priorities.
see this image:
https://lh3.googleusercontent.com/proxy/hClEn_tpXjmPgIEPbsV6a_QyPwsps7vVylAjGAc81nm-h2YOik56grbF_1qWK1AMyPFst5V0Poe6fv2nVCP1V3gbxaaaoRZncaAcC1gIJDUH8kF5Z47_BXUT02-12F67igvketU
- 1
the program wont work because you are using a++ inside of an operation
- 1
71