Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Binary operators order of precedence

In what order are the following parameters tested (in C++)?

if (a || b && c)
{
}

I've just seen this code in our application and I hate it, I want to add some brackets to just clarify the ordering. But I don't want to add the brackets until I know I'm adding them in the right place.

Edit: Accepted Answer & Follow Up

This link has more information, but it's not totally clear what it means. It seems || and && are the same precedence, and in that case, they are evaluated left-to-right.

http://msdn.microsoft.com/en-us/library/126fe14k.aspx

like image 587
Mark Ingram Avatar asked Dec 22 '22 14:12

Mark Ingram


1 Answers

[http://www.cppreference.com/wiki/operator_precedence] (Found by googling "C++ operator precedence")

That page tells us that &&, in group 13, has higher precedence than || in group 14, so the expression is equivalent to a || (b && c).

Unfortunately, the wikipedia article [http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence] disagrees with this, but since I have the C89 standard on my desk and it agrees with the first site, I'm going to revise the wikipedia article.

like image 124
Rodrigo Queiro Avatar answered Jan 04 '23 00:01

Rodrigo Queiro