Operator Precedence

The order of evaluation of operators

Operators Associativity

:: left to right   HIGH

() [] -> . left to right
++(postfix) --(postfix) right to left

++(prefix) --(prefix) left to right
! +(unary) -(unary) * & (type) right to left

* / % left to right   MEDIUM

+(binary) -(binary) left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= += -= *= /= %= right to left

?: right to left   LOW

, left to right


Unary +, - and * have precedence over the binary forms.  Operator () refers to a function call.

Precedence determines the order in which operands are bound to operators.  Operators on the same line have the same precedence; rows are in order of decreasing precedence.  C does not specify the order in which the operands of an operator are evaluated.  Similarly, the order in which function arguments are evaluated is not specified.  Examples of code that should be rewritten:

 x = f() + g();
 a[i] = i++;
 printf("%d %d\n",++n,power(2,n));
 z = x / ++x;

Standard Guarantees

A program should not depend on the order of evaluation of operands in an expression, except as guaranteed by the standard for the following operators.  All of these guarantee that expression a will be computed before expression b (or c).

 1.  a, b           comma operator (not the comma between arguments)
 2.  a && b         logical and
 3.  a || b         logical or
 4.  a ? b : c      conditional

In a function-call all arguments are evaluated before control transfers to the function.

 5.  a(b)           function call

Each full expression is evaluated before the next expression is evaluated.

 6.  each expression that is not part of another expression

   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page
Previous Reading  Previous: Bibliography Next: ASCII Collating Sequence   Next Reading

  Designed by Chris Szalwinski   Copying From This Site   

Creative Commons License