Search code examples
clogical-operatorsoperator-precedence

Logical operators precedence in C


Do logical operators have precedence in C (like && being executed after || or vice-versa) or do they execute from left to right? I usually use parentheses to make sure they execute correctly, but someone asked me about this and I was not sure.

For example is a || b && c == 2 interpreted as (((a || b) && c) == 2) or (a ||( b && (c == 2)))


Solution

  • The condition is interpreted as (a ||( b && (c == 2)))

    The && have a higher priority over the ||, as you can see here : https://en.cppreference.com/w/c/language/operator_precedence