This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Precedence and order of evaluation

  • 7 contributors

The precedence and associativity of C operators affect the grouping and evaluation of operands in expressions. An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first. Precedence can also be described by the word "binding." Operators with a higher precedence are said to have tighter binding.

The following table summarizes the precedence and associativity (the order in which the operands are evaluated) of C operators, listing them in order of precedence from highest to lowest. Where several operators appear together, they have equal precedence and are evaluated according to their associativity. The operators in the table are described in the sections beginning with Postfix Operators . The rest of this section gives general information about precedence and associativity.

Precedence and associativity of C operators

1 Operators are listed in descending order of precedence. If several operators appear on the same line or in a group, they have equal precedence.

2 All simple and compound-assignment operators have equal precedence.

An expression can contain several operators with equal precedence. When several such operators appear at the same level in an expression, evaluation proceeds according to the associativity of the operator, either from right to left or from left to right. The direction of evaluation does not affect the results of expressions that include more than one multiplication ( * ), addition ( + ), or binary-bitwise ( & , | , or ^ ) operator at the same level. Order of operations is not defined by the language. The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result.

Only the sequential-evaluation ( , ), logical-AND ( && ), logical-OR ( || ), conditional-expression ( ? : ), and function-call operators constitute sequence points, and therefore guarantee a particular order of evaluation for their operands. The function-call operator is the set of parentheses following the function identifier. The sequential-evaluation operator ( , ) is guaranteed to evaluate its operands from left to right. (The comma operator in a function call is not the same as the sequential-evaluation operator and does not provide any such guarantee.) For more information, see Sequence points .

Logical operators also guarantee evaluation of their operands from left to right. However, they evaluate the smallest number of operands needed to determine the result of the expression. This is called "short-circuit" evaluation. Thus, some operands of the expression may not be evaluated. For example, in the expression

x && y++

the second operand, y++ , is evaluated only if x is true (nonzero). Thus, y is not incremented if x is false (0).

The following list shows how the compiler automatically binds several sample expressions:

In the first expression, the bitwise-AND operator ( & ) has higher precedence than the logical-OR operator ( || ), so a & b forms the first operand of the logical-OR operation.

In the second expression, the logical-OR operator ( || ) has higher precedence than the simple-assignment operator ( = ), so b || c is grouped as the right-hand operand in the assignment. Note that the value assigned to a is either 0 or 1.

The third expression shows a correctly formed expression that may produce an unexpected result. The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- . However, if q && r evaluates to a nonzero value, s-- is not evaluated, and s is not decremented. If not decrementing s would cause a problem in your program, s-- should appear as the first operand of the expression, or s should be decremented in a separate operation.

The following expression is illegal and produces a diagnostic message at compile time:

In this expression, the equality operator ( == ) has the highest precedence, so p == 0 is grouped as an operand. The conditional-expression operator ( ? : ) has the next-highest precedence. Its first operand is p == 0 , and its second operand is p += 1 . However, the last operand of the conditional-expression operator is considered to be p rather than p += 2 , since this occurrence of p binds more closely to the conditional-expression operator than it does to the compound-assignment operator. A syntax error occurs because += 2 does not have a left-hand operand. You should use parentheses to prevent errors of this kind and produce more readable code. For example, you could use parentheses as shown below to correct and clarify the preceding example:

( p == 0 ) ? ( p += 1 ) : ( p += 2 )

C operators

Was this page helpful?

Additional resources

cppreference.com

Order of evaluation.

Order of evaluation of any part of any expression, including order of evaluation of function arguments is unspecified (with some exceptions listed below). The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again.

There is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression a ( ) + b ( ) + c ( ) is parsed as ( a ( ) + b ( ) ) + c ( ) due to left-to-right associativity of operator+, but c ( ) may be evaluated first, last, or between a ( ) or b ( ) at run time:

Possible output:

[ edit ] "Sequenced before" rules (since C++11)

[ edit ] evaluation of expressions.

Evaluation of each expression includes:

  • Value computations : calculation of the value that is returned by the expression. This may involve determination of the identity of the object (glvalue evaluation, e.g. if the expression returns a reference to some object) or reading the value previously assigned to an object (prvalue evaluation, e.g. if the expression returns a number, or some other value).
  • Initiation of side effects : access (read or write) to an object designated by a volatile glvalue, modification (writing) to an object, calling a library I/O function, or calling a function that does any of those operations.

[ edit ] Ordering

Sequenced before is an asymmetric, transitive, pair-wise relationship between evaluations within the same thread.

  • If A is sequenced before B (or, equivalently, B is sequenced after A), then evaluation of A will be complete before evaluation of B begins.
  • If A is not sequenced before B and B is sequenced before A, then evaluation of B will be complete before evaluation of A begins.
  • Evaluations of A and B are unsequenced : they may be performed in any order and may overlap (within a single thread of execution, the compiler may interleave the CPU instructions that comprise A and B).
  • Evaluations of A and B are indeterminately sequenced : they may be performed in any order but may not overlap: either A will be complete before B, or B will be complete before A. The order may be the opposite the next time the same expression is evaluated.

[ edit ] Rules

[ edit ] undefined behavior.

1) If a side effect on a memory location is unsequenced relative to another side effect on the same memory location, the behavior is undefined .

2) If a side effect on a memory location is unsequenced relative to a value computation using the value of any object in the same memory location, the behavior is undefined .

[ edit ] Sequence point rules (until C++11)

[ edit ] pre-c++11 definitions.

Evaluation of an expression might produce side effects, which are: accessing an object designated by a volatile lvalue, modifying an object, calling a library I/O function, or calling a function that does any of those operations.

A sequence point is a point in the execution sequence where all side effects from the previous evaluations in the sequence are complete, and no side effects of the subsequent evaluations started.

[ edit ] Pre-C++11 Rules

1) There is a sequence point at the end of each full-expression (typically, at the semicolon).

2) When calling a function (whether or not the function is inline and whether or not function call syntax was used), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body.

3) When returning from a function, there is a sequence point after the copy-initialization of the result of the function call, and before the destruction of all temporary objects at the end of expression in the return statement (if any).

4) There is a sequence point after the copying of a returned value of a function and before the execution of any expressions outside the function.

5) Once the execution of a function begins, no expressions from the calling function are evaluated until execution of the called function has completed (functions cannot be interleaved).

6) In the evaluation of each of the following four expressions, using the built-in (non-overloaded) operators, there is a sequence point after the evaluation of the expression a .

[ edit ] Pre-C++11 Undefined behavior

1) Between the previous and next sequence point, the value of any object in a memory location must be modified at most once by the evaluation of an expression, otherwise the behavior is undefined .

2) Between the previous and next sequence point, for any object in a memory location, its prior value that is modified by the evaluation of the expression must be accessed only to determine the value to be stored. If it is accessed in any other way, the behavior is undefined .

[ edit ] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

[ edit ] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 6.9.1 Program execution [intro.execution]
  • 7.6.1.6 Increment and decrement [expr.post.incr]
  • 7.6.2.8 New [expr.new]
  • 7.6.14 Logical AND operator [expr.log.and]
  • 7.6.15 Logical OR operator [expr.log.or]
  • 7.6.16 Conditional operator [expr.cond]
  • 7.6.19 Assignment and compound assignment operators [expr.ass]
  • 7.6.20 Comma operator [expr.comma]
  • 9.4.5 List-initialization [dcl.init.list]
  • C++20 standard (ISO/IEC 14882:2020):
  • 7.6.1.5 Increment and decrement [expr.post.incr]
  • 7.6.2.7 New [expr.new]
  • 9.4.4 List-initialization [dcl.init.list]
  • C++17 standard (ISO/IEC 14882:2017):
  • 4.6 Program execution [intro.execution]
  • 8.2.6 Increment and decrement [expr.post.incr]
  • 8.3.4 New [expr.new]
  • 8.14 Logical AND operator [expr.log.and]
  • 8.15 Logical OR operator [expr.log.or]
  • 8.16 Conditional operator [expr.cond]
  • 8.18 Assignment and compound assignment operators [expr.ass]
  • 8.19 Comma operator [expr.comma]
  • 11.6.4 List-initialization [dcl.init.list]
  • C++14 standard (ISO/IEC 14882:2014):
  • 1.9 Program execution [intro.execution]
  • 5.2.6 Increment and decrement [expr.post.incr]
  • 5.3.4 New [expr.new]
  • 5.14 Logical AND operator [expr.log.and]
  • 5.15 Logical OR operator [expr.log.or]
  • 5.16 Conditional operator [expr.cond]
  • 5.17 Assignment and compound assignment operators [expr.ass]
  • 5.18 Comma operator [expr.comma]
  • 8.5.4 List-initialization [dcl.init.list]
  • C++11 standard (ISO/IEC 14882:2011):

[ edit ] See also

  • Operator precedence which defines how expressions are built from their source code representation.
  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 5 November 2023, at 06:45.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

IMAGES

  1. Solved Assignment operator is evaluated Left to Right.

    assignment operator is evaluated left to right

  2. PPT

    assignment operator is evaluated left to right

  3. PPT

    assignment operator is evaluated left to right

  4. Assignment Operator in C Programming

    assignment operator is evaluated left to right

  5. Chapter 2 Data and Expressions

    assignment operator is evaluated left to right

  6. PPT

    assignment operator is evaluated left to right

VIDEO

  1. Data entry operator... 5 days left to application

  2. C programming/ shift operator / Right shift / Left shift

  3. Core

  4. C programming / Program / #define / printf

  5. Assignment Operator In Python

  6. 13. Operator Precedence in Python