Why the out put of this code is 71? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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

10th Apr 2020, 5:58 AM
Ehsan ghazanfary
13 Antworten
+ 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
10th Apr 2020, 7:12 AM
Jayakrishna 🇮🇳
+ 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
10th Apr 2020, 6:13 AM
Jayakrishna 🇮🇳
+ 2
what was your desired output?
10th Apr 2020, 6:03 AM
Enrique Flores
Enrique Flores - avatar
+ 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
10th Apr 2020, 6:14 AM
Ehsan ghazanfary
+ 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
11th Apr 2020, 6:25 AM
Jai Verma
Jai Verma - avatar
+ 1
Due to binary precedence. U have to take care of precidence operator
12th Apr 2020, 5:24 AM
Vivekanand Bhardwaj
Vivekanand Bhardwaj - avatar
0
you should be using a + 1
10th Apr 2020, 6:09 AM
Enrique Flores
Enrique Flores - avatar
0
72
10th Apr 2020, 6:09 AM
Ehsan ghazanfary
10th Apr 2020, 6:09 AM
Enrique Flores
Enrique Flores - avatar
0
check my code👆
10th Apr 2020, 6:11 AM
Enrique Flores
Enrique Flores - avatar
10th Apr 2020, 6:22 AM
Ehsan ghazanfary
- 1
the program wont work because you are using a++ inside of an operation
10th Apr 2020, 6:08 AM
Enrique Flores
Enrique Flores - avatar
- 1
71
11th Apr 2020, 7:48 PM
Lihle Zonela
Lihle  Zonela - avatar