(page ported, too lazy to convert table to a wiki format) |
m (Inject moved page Operator precedence and associativity - C to c/operator precedence and associativity) |
(No difference)
|
Revision as of 06:15, 4 January 2015
Operators in C follow strict precedence rules defined by the C standard. The table below lists C language operators in order of precedence from highest to lowest.
Operators
Precedence level | Associativity | Operators | Description | Since | Type |
---|---|---|---|---|---|
1 (highest) | left to right | ( ) | Function call | C89 | Postfix operators |
[ ] | Array subscripting | ||||
. | Structure and union member access | ||||
-> | Structure and union member access (through pointer) | ||||
(type-name){list} | Compound literal | C99 | |||
++ | Postfix increment | C89 | |||
-- | Postfix decrement | ||||
2 | right to left | ++ | Prefix increment | Unary operators | |
-- | Prefix decrement | ||||
sizeof | Size-of | ||||
_Alignof | Align-of | C11 | |||
+ | Unary plus | C89 | |||
- | Unary minus | ||||
* | Dereference | ||||
! | Logical NOT | ||||
~ | Bitwise NOT | ||||
& | Address-of | ||||
(type-name) | Type cast | Cast operators | |||
3 | left to right | * | Multiplication | Multiplicative operators | |
/ | Division | ||||
% | Modulo | ||||
4 | + | Addition | Additive operators | ||
- | Subtraction | ||||
5 | << | Bitwise left shift | Bitwise shift operators | ||
>> | Bitwise right shift | ||||
6 | < | Less than | Relational operators | ||
> | Less than or equal to | ||||
<= | Greater than | ||||
>= | Greater than or equal to | ||||
7 | == | Equal to | Equality operators | ||
!= | Not equal to | ||||
8 | & | Bitwise AND | Bitwise AND operator | ||
9 | ^ | Bitwise exclusive OR | Bitwise exclusive OR operator | ||
10 | | | Bitwise inclusive OR | Bitwise inclusive OR operator | ||
11 | && | Logical AND | Logical AND operator | ||
12 | || | Logical OR | Logical OR operator | ||
12 | right to left | ?: | Ternary conditional | Conditional operator | |
13 | = | Assignment | Assignment operators | ||
+= | Assignment by sum | ||||
-= | Assignment by difference | ||||
*= | Assignment by product | ||||
/= | Assignment by quotient | ||||
<<= | Assignment by bitwise left shift | ||||
>>= | Assignment by bitwise right shift | ||||
%= | Assignment by Modulo | ||||
&= | Assignment by bitwise AND | ||||
^= | Assignment by bitwise exclusive OR | ||||
|= | Assignment by bitwise inclusive OR | ||||
14 | left to right | , | Comma | Comma operator |
Expressions
An expression is a sequence of operators, operands, object designators, or functions that specifies computation of a value. An object's stored value may only be modified once between any two sequence points. This is important to watch out for because it means the following are undefined:
x = ++x + 1;
arr[x++] = x;
++x = ++x;
While allowing the following:
x = x + 1;
arr[x] = x;
x = ++x;
is well defined.
Precedence and Evaluation
Precedence are rules used to define the order of binding in a sequence of expressions where they are not explicitly specified by a pair of parentheses. For example in,
++x+3*4
The table indicates that x is bound more tightly to ++ than to the + operator. Likewise, 3 is bound more tightly to * than to +. Therefore it is equivalent to:
((++x)+(3*4))
or
(++x, x+(3*4))
It's important to note that the actual order of evaluation is independent of precedence or associativity. In fact, the order of evaluation of subexpressions and the order in which side is evaluated is unspecified behavior by the C standard.
This means that in the following line,
x = f(1) + f(2) + f(3);
The function calls f(1), f(2), and f(3) may be called in any order. You may not assume that f(1) will be called before f(2) or f(3).