From the reference link below, I made this C operator table for quick reference.

Operator Table

PriorityCategoryOperatorAssociativity
1Unary Postfix
++ --

Suffix/postfix increment and decrement, e.g., var++;.

()

Function call, e.g., func (param).

[]

Array subscripting, e.g., x=X[1];.

.

Structure and union member access, e.g., x=Param.x;.

->

Structure and union member access through pointer, e.g., x=this->x;.

(type){list}

Compound literal (C99), e.g., int *p = (int[]){2, 4};

Left to right

Unary postfix operators always associate left-to-right (a[1][2]++ is ((a[1])[2])++). Note that the associativity is meaningful for member access operators, even though they are grouped with unary postfix operators: a.b++ is parsed (a.b)++ and not a.(b++).

2Unary Prefix
++ --

Prefix increment and decrement, e.g., ++var;.

+ -

Unary plus and minus, e.g., a=b+1;.

! ~

Logical NOT and bitwise NOT, e.g., a=!b;.

(type)

Type cast, e.g., float x = (float)y;.

*

Indirection (dereference), e.g., float x = *(float *)&y;.

&

Address-of, e.g., float x = *(float *)&y;.

sizeof

Size-of, e.g., sizeof (int) * p is (sizeof(int)) * p.

Right to left

Unary prefix operators always associate right-to-left (sizeof ++*p is sizeof(++(*p))).

3Binary
* / %

Multiplication, division, and remainder, e.g., x=a*b/c%d is x=((a*b)/c)%d.

Left to right
4Binary
+ -

Addition and subtraction, e.g., x=a+b-c is x=(a+b)-c.

Left to right
5Binary
<< >>

Bitwise left shift and right shift, e.g., x=a>>1<<1 is x=(a>>1)<<1.

Left to right
6Binary
< <=

For relational operators < and ≤ respectively.

> >=

For relational operators > and ≥ respectively.

Left to right
7Binary
== !=

For relational = and ≠ respectively.

Left to right
8Binary
&

Bitwise AND.

Left to right
9Binary
^

Bitwise XOR (exclusive or).

Left to right
10Binary
|

Bitwise OR (inclusive or).

Left to right
11Binary
&&

Logical AND.

Left to right
12Binary
||

Logical OR, e.g., x = a && b || c && d is x = (a&&b) || (c&&d).

Left to right
13Ternary
?:

Ternary conditional. The expression in the middle of the conditional operator (between ? and :) is parsed as if it is parenthesized: its precedence relative to ?: is ignored.

Right to left
14Assign
=

Simple assignment. The expression a=b=c is parsed as a=(b=c), and not as (a=b)=c because of right-to-left associativity.

+= -=

Assignment by sum and difference.

*= /= %=

Assignment by product, quotient, and remainder.

<<= >>=

Assignment by bitwise left shift and right shift.

&= ^= |=

Assignment by bitwise AND, XOR, and OR.

Right to left
15Comma
,

Comma.

Left to right

Reference

C Operator Precedence

Categories: CodingComputer

0 Comments

Leave a Reply

Your email address will not be published.