Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Precedence and order of evaluation
In this articleThe 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 ( Only the sequential-evaluation ( 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
the second operand, ExamplesThe following list shows how the compiler automatically binds several sample expressions:
In the first expression, the bitwise-AND operator ( In the second expression, the logical-OR operator ( The third
expression shows a correctly formed expression that may produce an unexpected result. The logical-AND operator ( The following expression is illegal and produces a diagnostic message at compile time:
In this expression, the equality operator (
See alsoC operators FeedbackSubmit and view feedback for Additional resourcesWhich operators are used to determine the relationship between different operands?Relational operators are important for making decisions. They allow us compare numeric and char (chars are treated like numbers in C++) values to determine if one is greater than, less than, equal to, or not equal to another. Relational operators are binary meaning they require two operands.
Which operators are used to join two or more relation?Logical operators connect two or more relational expressions into one or reverse the logic of an expression. We use logical operators to check whether an expression is true or false.
Which operator is used to know the relationship between two values?(b) Relational operators compare the relationship between two values or arguments.
|