Shell Shell

C's Operators

 

Precedence
level
Type
Operators
Associativity
direction
1 primary ( ) [ ] -> . ++ -- left-to-right
2 unary ! ~ ++ -- + - * & (type) sizeof right-to-left
3 binary * / % left-to-right
4 binary + - left-to-right
5 binary << >> left-to-right
6
binary < <= > >= left-to-right
7 binary ==!= left-to-right
8 binary & left-to-right
9 binary ^ left-to-right
10 binary | left-to-right
11 binary && left-to-right
12 binary || left-to-right
13 ternary ?: right-to-left
14 binary = += -= *= /= %= &= ^= |= <<= >>= right-to-left
15 binary , left-to-right

Notes:

  1. Operations with the highest precedence (level 1) are performed first, those with the lowest precedence (level 15) are performed last.
  2. All operators at the same level have equal precedence.
  3. For operators at the same level of precedence, the order in which operations are performed is determined by the direction of their associativity.
  4. A unary operator operates on a single object and is prefixed to it, e.g. &a
    A binary operator operates on two objects and is placed between them, e.g. a * b
    The only ternary operator operates on three objects, viz. a ? b : c
  5. The postfix (primary) versions of ++ and -- have higher precedence than the prefix (unary) versions.
  6. The unary versions of +, -, * and & have higher precedence than the binary versions.
David C. Hamill
D.Hamill@surrey.ac.uk

Last updated 09/06/2023 14:50:34