C Operators and Precedence

Based on "The C Programming Language" by Kernighan and Ritchie, and various web resources such as this C Language Reference Manual (may be a dead link). This page includes C++ information from Wikipedia; it is available for use under the terms of the GNU Free Documentation License.

This table lists all the C operators (and additional C++ operators), in order of evaluation precedence. Associativity describes the order of evaluation for multiple operators with equal precedence.

Precedence
(0 is highest,
15 is lowest)
Operator Associativity Remarks
0 (variables, constants, string literals, parenthesized expressions) aka "atoms"
0 :: Scope resolution left-to-right C++ only
1 fd ( argslist ) function call left-to-right postfix expressions
arr [ idx ] array access
sp -> elt (indirect) element selection through pointer
s . elt (direct) element selection by reference
lvalue++   lvalue-- post-increment ,   post-decrement
Unary
2 ! expr logical negation ("NOT") right-to-left  
~ expr bit-wise Inverse (1s' complement)  
++lvalue   pointer to storage pre-increment ,   pre-decrement  
+ expr identity (unary plus)  
- expr arithmetic negation (unary minus)  
* Pexpr dereference (``contents-of'')  
& lvalue address-of  
( type  ) cast  
sizeof object storage-size compile-time operator
Dynamic memory control
2 new   new[ ] allocate storage right-to-left C++ only — C uses malloc(), free()
delete   delete[ ] Deallocate previously allocated memory
Arithmetic
3 expr * expr2 multiplication left-to-right  
expr / expr2 division type-sensitive
expr % expr2 modulo (remainder)  
4 expr + expr2 addition left-to-right  
expr - expr2 subtraction  
5 expr << expr2 bit-shift left left-to-right "integer"-oriented
expr >> expr2 bit-shift right
Relational
6 expr < expr2 less than left-to-right  
expr <= expr2 less or equal to  
expr > expr2 greater than  
expr >= expr2 greater or equal to  
Equivalence
7 expr == expr2 is equal to left-to-right  
expr != expr2 is not equal to  
Bit-wise
8 expr & expr2 bit-wise AND left-to-right  
9 expr ^ expr2 bit-wise XOR  
10 expr | expr2 bit-wise OR  
Boolean
11 expr && expr2 logical conjunction ("AND") left-to-right  
12 expr || expr2 logical disjunction ("OR")  
13 Cexpr ? Texpr : Fexpr "choice" right-to-left evaluate and return Texpr or Fexpr, depending on the value of Cexpr
Assignments
14 expr = expr2 Assignment right-to-left side effects; LHS must be an lvalue
expr += expr2 additive assignment
expr -= expr2 subtractive assignment
expr *= expr2 multiplicative assignment
expr /= expr2 dividing assignment
expr %= expr2 modulo assignment
expr &= expr2 bit-AND assignment
expr ^= expr2 bit-XOR assignment
expr |= expr2 bit-OR assignment
expr <<= expr2 shift-left assignment
expr >>= expr2 right-left assignment
15 expr , expr2 Comma (sequential evaluation) left-to-right expr is evaluated (for side effects) and discarded, then expr2 is evaluated

Notes

lvalue An expression that evaluates to a reference to storage (modifiable portion of memory).
Pexpr An expression that evaluates to a pointer to a function or lvalue.
fd A postfix expression that identifies a function — e.g., a function name.
argslist A comma-separated list of function arguments. May be empty.
arr A postfix expression that identifies an array; it evaluates as a pointer to a type. (Formally, arr and idx are interchangeable.)
idx An expression that evaluates to an integer type, and serves to locate an element within an array. (Formally, arr and idx are interchangeable.)
sp A postfix expression that evaluates as a pointer to a structure or union.
s A postfix expression that evaluates as a a structure or union.
elt An expression that evaluates to a member of the structure or union.
type A valid C type — int, char, double, etc.
object The operand is parenthesized type-name, or expression whose value occupies a known amount of memory.
Cexpr Conditional expression — evaluates to "true" (1) or "false" (0)
Texpr True-expression — is evaluated if and only if the governing conditional expression is "true"
Fexpr False-expression — is evaluated if and only if the governing conditional expression is "false"
exprexpr2 Legal expressions or "atoms".

Python Operators and Precedence

Based on the Python Language Reference.

This table lists all the Python operators, in order of evaluation precedence. Associativity describes the order of evaluation for multiple operators with equal precedence.

Precedence
(0 is highest,
16 is lowest)
Operator Associativity Remarks
0 (variables, constants, string literals) aka "atoms"
1 (expressions...) Binding, or tuple display.
[expressions...] List display.
{key:datum...} Dictionary display.
{datum...} Set display.
`expressions...` String conversion. Not in Python 3.
2 fd ( argslist ) function call left-to-right  
arr [ idx ] collection access "collection" is a tuple, list, or dictionary
arr [ idx1:idx2 ] collection slicing "collection" is a tuple or list
s . attribute attribute reference
exponentiation
3 expr ** expr exponentiation right-to-left  
Unary
4 ~ expr bit-wise Inverse left-to-right 1s' complement
+ expr unary plus identity
- expr unary minus arithmetic negation
Arithmetic
5 expr * expr2 multiplication left-to-right  
expr / expr2 division Python 2.x: type-sensitive; Python 3.x: real division
expr // expr2 integer division  
expr % expr2 modulo remainder also used for formatting, with same precedence
6 expr + expr2 addition left-to-right  
expr - expr2 subtraction  
7 expr << expr2 bit-shift left left-to-right "integer"-oriented
expr >> expr2 bit-shift right
Bit-wise
8 expr & expr2 bit-wise AND left-to-right  
9 expr ^ expr2 bit-wise XOR  
10 expr | expr2 bit-wise OR  
Relational
11 expr < expr2 less than left-to-right  
expr <= expr2 less or equal to  
expr > expr2 greater than  
expr >= expr2 greater or equal to  
expr == expr2 is equal to  
expr != expr2 is not equal to  
expr in expr2 inclusion in a collection  
expr not in expr2 exclusion from a collection  
expr is expr2 identity test  
expr is not expr2 non-identity test  
Boolean
12 not expr logical negation left-to-right  
13 expr and expr2 logical conjunction  
14 expr or expr2 logical disjunction  
other
15 if – else conditional evaluation  
16 lambda argslist : expr lambda expression define and apply an anonymous function

Notes

fd A name that identifies a function.
argslist A comma-separated list of function arguments. May be empty.
arr A name that identifies a tuple, list, or dictionary.
idx An expression that evaluates to an integer type or a dictionary key, and serves to locate an element within a tuple, list, or dictionary.
exprexpr2 Legal expressions or "atoms".

Pascal Operator Precedence

Operator precedence in Pascal is much simpler than in C. This table is based on:

Precedence Operator Associativity Remarks
Highest not Bexpr logical negation left to right Bexpr must evaluate to a Boolean value
next-higher expr * expr2 multiplication expr, expr2 must evaluate to numerical values
expr / expr2 real division
expr div expr2 integer division
expr mod expr2 modulo (remainder)
Bexpr and Bexpr2 logical conjunction Bexpr, Bexpr2 must evaluate to Boolean values
next-lower expr + expr2 addition expr, expr2 must evaluate to numerical values
expr - expr2 subtraction
Bexpr or Bexpr2 logical disjunction Bexpr, Bexpr2 must evaluate to Boolean values
Lowest expr = expr2 is equal to expr, expr2 must evaluate to numerical values
expr <> expr2 is not equal to
expr < expr2 less than
expr <= expr2 less or equal to
expr > expr2 greater than
expr >= expr2 greater or equal to
expr in Sexpr is a member of Sexpr must evaluate to a set

More compactly stated:

Precedence Operators Associativity
Highest not left to right
. *   /   div   mod   and
. +   -   or
Lowest =   <>   <   <=   >   >=   in