Computing the value of an expression in infix notation is a complex process because it must take into account the relative precedence (i.e., priority) of the different operators. For example, the expression x + y * z must be interpreted as x + (y * z), i.e., first compute the value of y * z and then add the result to x.
The table below shows the C language operators in decreasing order of priority: when computing the value of an infix expression, the operators on the first row are executed first, and the operators on the last row are executed last.
The operators on the second row of the table
are unary (only one operand)
while all the others are binary (two operands).
The right column indicates the association rule
for the operators on the row:
L-R means from left to right
and
R-L
means from right to left
.
For exemple,
v * w / x * y % 10 * z
is interpreted as
((((v * w) / x) * y) % 10) * z.
() [] -> . | L-R |
- ++ -- ! & * ~ (type) sizeof | R-L |
* / % | L-R |
+ - | L-R |
<< >> | L-R |
< <= >= > | L-R |
== != | L-R |
& | L-R |
^ | L-R |
| | L-R |
&& | L-R |
|| | L-R |
? : | R-L |
= op = | R-L |
, | L-R |
The penultimate row
of the table refers to expressions like
x = y
and
x += y
.
Since the association is right-to-left,
an expression like x = y = 100
is interpreted as
x = (y = 100).
The last row of the table refers to the comma operator that figures in expressions like
x = a+1, y = b+2;and has essentially the same effect as {x = a+1; y = b+2;}.
Here are a few more examples:
expression | interpretation |
---|---|
&x[i] | &(x[i]) |
*p.dia | *(p.dia) |
a[i].b[j] | ((a[i]).b)[j] |
h->e->d | (h->e)->d |
&h->e | &(h->e) |
*x++ | *(x++) |