Table of operator precedence
As a rule, all statements are executed from left to right, but there is such a concept as statement priority. If the next statement has a higher priority, the statement with a higher priority is executed first. For example, multiplication has a higher priority and 4 + 5 * 2 is 14, but if we use parentheses, ( 4 + 5 ) * 2 is 18.
Character operation | Associativity |
The highest priority | |
() [] . ~ -> | Left to right |
! &(un) *(un) -(un) ~(un) ++ -- @(un) | Right to left |
% * / | Left to right |
+ - @ | Left to right |
<< >> | Left to right |
< > <= >= %< %> %<= %>= | Left to right |
!= == %== %!= | Left to right |
& | Left to right |
^ | Left to right |
| | Left to right |
&& | Left to right |
|| | Left to right |
?(,,) | Left to right |
= += -= *= /= %= &= |= ^= >>= <<= as | Right to left |
The lowest priority |
Parentheses () change the order in which expressions are evaluated. You can use square brackets in order to deal with the elements of the array or the indexed elements, for example, a character in a string. The unary operators include !, &, *, -, ~, ++, --. It is the prefix notation that is used for all unary operators, except increments. As for increment operations ++ and --, they can be occured either in the prefix or in the postfix notation. The following operators &, *, -, @, ~ are likely to be binary as well as unary operators. All other operators are binary (taking two operands).