This is a list of the C operators, their precedence, associativity and what they do.
• operators are listed in increasing order of precedence, ie, operators towards the bottom of the list bind closer that operators towards the top of the list. So, `2 + 3 * 6` is evaluated as `2 + (3 * 6)`, since `*` has a higher precidence (is closer to the bottom) than `+`. Operators with the same precedence are grouped in the same table entry.
expr1 , expr2 lvalue1 = expr1 Comma (left assoc) Used to separate expressions: ` b = a + 3, c = b * 2 + 1` assigns `a + 3` to `b` and assigns `b * 2 + 1` to `c`. expr1 is evaluated first, then expr2; the value of the expression is that of expr2. Assignment (right assoc) Assigns the value of expr1 to lvalue1: ` a = 3 * 4 ` expr1 is evaluated before lvalue1. The `=` can be preceded by any binary arithmetic operator, eg, lvalue1 `+=` expr1 ` += 3 ` which is (almost) equivalent to lvalue1 `=` lvalue1 `+` (expr1) (almost because lvalue1 is evaluated once, not twice). Valid assignment operators are: = += -= *= /= %= &= |= ^= <<= >>= Conditional (left assoc) If expr1 evaluates to a non-zero value, expr2 is evaluated, otherwise expr3 is evaluated. The value of the expression as a whole is which ever of expr2 or expr3 is evaluated (this means the type of expr2 and expr3 must be the same). Example: ` a = b < 0 ? -b : b` `a` is assigned the absolute value of `b`. Logical Or (left assoc) Evaluates to true (1) if either expr1 or expr2 evaluates to true (non-zero), otherwise evaluates to false (zero). expr2 is evaluated if and only if expr1 evaluates to false (zero). Example: ` a == 0 || 10 / a == 0` returns true if `a` is zero or if `10 / a` is zero - note that `10 / a` is not evaluated if `a` is 0. Logical And (left assoc) Evaluates to true (1) if both expr1 and expr2 evaluate to true (non-zero), otherwise evaluates to false (zero). expr2 is evaluated if and only if expr1 evaluates to true (non-zero). Example: ` a != 0 && 10 / a != 0` returns true if `a` is non-zero and if `10 / a` is non-zero - note that `10 / a` is not evaluated if `a` is 0. Bitwise Or (left assoc) Returns the bit-wise or of expr1 and expr2 Example: ` 0x12 | 0x58` returns 0x5A (all the set bits of 0x12 and 0x58 are combined). Bitwise Exclusive-or (left assoc) Returns the bit-wise exclusive-or (xor) of expr1 and expr2 Example: ` 0x12 ^ 0x58` returns 0x4A (the set bits in the result are those that are set in either 0x12 or 0x58, but not set in both). Bitwise And (left assoc) Returns the bit-wise and of expr1 and expr2 Example: ` 0x12 & 0x58` returns 0x10 (the set bits in the result are those that are set in both 0x12 and 0x58). Equality,Inequality (left assoc) `==` returns true (1) if expr1 and expr2 are equal, returns false (0) otherwise; `!=` returns true (1) if expr1 and expr2 are not equal, returns false (0) otherwise. Example: ` a + 2 == b * 5 ` NOTE: A common mistake is to use `=` as an equality test instead of `==`. Comparison (left assoc) `<=` returns true (1) if expr1 is less than or equal to expr2, returns false (0) otherwise; `>=` returns true (1) if expr1 is greater than or equal to expr2, returns false (0) otherwise; `<` returns true (1) if expr1 is strictly less than to expr2, returns false (0) otherwise; `>` returns true (1) if expr1 is strictly greater than to expr2, returns false (0) otherwise. Example: ` a + 2 <= b * 5 ` Left Shift Right Shift (left assoc) `<<` returns expr1 shifted left expr2 bit positions; 0 bits are shifted into the low order bits. Mathematically equivalent to expr1 * 2expr2 (note that there is no exponentiation operator in C, nor in netscape 1.1n :-)). `>>` returns expr1 shifted right expr2 bit positions; if the type of expr1 is signed, 1 bits are shifted into the high order bits, otherwise 0 bits are shifted in. Mathematically equivalent to expr1 / 2expr2. Example: ` b = 0x12 << 2 ` b is assigned the value 0x48. Addition Subtraction (left assoc) `+` returns expr1 plus expr2; if the type of one of the expressions is a pointer type, a pointer to the ith element is returned, where i is the other expression. Ie, `p + 10` is the same as `&((p)[10])`. `-` returns expr1 minus expr2; if the type of one of the expressions is a pointer type, a pointer to the -ith element is returned, where i is the other expression. Ie, `p - 10` is the same as `&((p)[-10])`. Example: ` 2 + 3 - 10` returns 11. Multiplication Division Modulus (left assoc) `*` returns expr1 times expr2. `/` returns expr1 divided by expr2; if expr1 and expr2 are both integer expressions, the result is also an integer (ie, result is truncated). If expr2 evaluates to zero, an exception will be generated (ie, the program will dump core). `%` returns the remainder of expr1 divided by expr2; both expr1 and expr2 must be integer expressions. If expr2 evaluates to zero, an exception will be generated (ie, the program will dump core). Example: ` 4 * 3 ` ` 14 / 3 ` ` 14 % 3 ` return 12, 4 and 2, respectively. De-reference Address-of Negation no-op Bitwise Compliment Logical Compliment Pre-Increment Post-Increment Pre-Decrement Post-Decrement Type Cast (right assoc) `*` returns what expr1 points to `&` returns the address of lvalue1. `-` returns the negation of expr1. `+` returns expr1 (does nothing, added by ANSI to provide symmetry with the unary `-` operator). `~` returns the bitwise compliment of expr1, ie, all the bits are flipped. `!` returns the logical compliment of expr1: if expr1 evaluates to true (non-zero), it it returns 0; otherwise it returns 1. `++` the pre-increment version adds 1 to lvalue1 and returns the result; the post-increment version adds 1 to lvalue1 and returns the original value of lvalue1. If lvalue1 is a pointer, the size of the type pointed to is added instead of 1 (ie, the pointer will be incremented enough to point to the next object). `--` Like `++`, except subtraction is used. `(`type-cast`)` the type of is coerced to the type indicated by type-cast. Example: (int) 2.3 results in an integer-type expression with the value 2. Structure De-reference Structure Reference Array Reference Parentheses (left assoc) `->` returns the value of field in the structure pointed to by expr1. `.` returns the value of field in the structure lvalue1. `[`...`]` returns the expr2th element of the `array' pointed to by expr1. Exactly equivalent to: `*(`expr1 `+ (`expr2`))` `(`...`)` result returned is that of expr1. Used to override the default precedence. Example: ` 3 * (2 + 3) ` returns 15.