Appendix G
Order of Precedence
- The order of precedence goes as shown in Table G-1 and is overridden by using parentheses. C always executes the portion of an equation in parentheses before anything else.
- Incremented or decremented variables as lvalues (assigned to another value) operate left-to-right. So,
++varincrements before its value is assigned;var++increments its value after it's assigned. (See Table G-2.) - Associativity for the assignment operators moves right-to-left. For example, the operation on the right side of
+=happens first. - The order of precedence may also be referred to as the order of operations.
TABLE G-1 Standard Operator Precedence
|
Operator(s) |
Category |
Description |
|---|---|---|
|
() [] -> . (period) |
Expression |
Function arguments, arrays, pointer members |
|
! ~ -- + * & ++ -- |
Unary |
Logical not, one’s complement, positive, negative, pointer, address-of, increment, decrement; operations right-to-left |
|
* / % |
Math |
Multiplication, division, modulo |
|
+ – |
Math |
Addition, subtraction |
|
<< >> |
Binary |
Shift left, shift right |
|
< > <= >= |
Comparison |
Less than, greater than, less than or equal to, greater than or equal to |
|
== != |
Comparison |
Is equal to, not equal to |
|
& |
Binary |
And |
|
^ |
Binary |
Exclusive or (XOR) |
|
| |
Binary |
Or |
|
&& |
Logical |
And |
|
|| |
Logical |
Or |
|
?: |
Comparison |
Ternary operator, associativity goes right-to-left |
|
= |
Assignment |
Variable assignment operator, including |
|
, (comma) ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access