+ 1

c++ operation order...

#include <iostream> using namespace std; int main() { cout<<(1||0); cout<<(1&&0); cout<<((1||0)&&0); cout<<(1||0&&0); return 0; } I can't understand why the output of third one and last one is different...sad...

25th Jan 2018, 6:48 AM
jae nong
jae nong - avatar
1 Odpowiedź
+ 1
The logical and (&&) operator has higher precedence than the logical or (||) operator. So (1||0)&&0 is 0, because 1||0 = 1 and 1&&0 = 0. On the next line, the and gets evaluated first, so you get 1||(0&&0), so 1||0 = 1. You can see the operator precedence table here: http://en.cppreference.com/w/cpp/language/operator_precedence
25th Jan 2018, 7:00 AM
SplittyDev
SplittyDev - avatar