+ 2

The weird result

Why does the following code outputs 8: #include <iostream> using namespace std; int main() { cout<< 3/3+7; return 0; }

14th Jan 2017, 5:31 PM
Emanuel Dajti
Emanuel Dajti - avatar
7 Respuestas
+ 24
3/3=1 1+7=8 operations like */% are carried out before +-
14th Jan 2017, 5:34 PM
Frost
Frost - avatar
+ 20
8
14th Jan 2017, 5:33 PM
Frost
Frost - avatar
+ 14
3/3+7 -> 1+7 -> 8 (operations on left are executed first)
14th Jan 2017, 5:34 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
/ has higher precedence than + . So 3/3=1 and 1+7= 8.
14th Jan 2017, 5:47 PM
Mr.Robot
Mr.Robot - avatar
+ 4
Ok but why
14th Jan 2017, 5:33 PM
Emanuel Dajti
Emanuel Dajti - avatar
+ 4
I should have thought about it..Thanks
14th Jan 2017, 5:35 PM
Emanuel Dajti
Emanuel Dajti - avatar
+ 4
BODMAS Rule comes into picture (Brackets of division, multiplication, addition, subtraction)
14th Jan 2017, 5:50 PM
Akshay S A