Operator Prescedence

This page was translated by a robot.

In C and C++, multiple operators can be listed in a single statement. The order in which the individual operators are processed is precisely specified by the language using processing rules.

Details

Basically, the processing rules specified by the languages ​​C and C++ correspond to an intuitive understanding of the semantic meanings of the operators, which normally do not cause any problems in everyday use of the programming language. Occasionally, however, these rules have quirks that sometimes lead to unwanted and hard-to-find errors. Both a ranking and a processing direction are specified for each operator, which determine the execution of the statement.

Ranking

A priority is specified for each operator by assigning a rank to each operator . This order of precedence (referred to as operator-precedence ) ensures the associativity of different operators based on their priority. Operators with a lower rank are given more priority than those with a higher rank. The highest priority rank is 1. On this page, an attempt was made to do justice to the most important sources and so a total of 19 ranks were distributed, which are listed in the table below.

For example, mathematical operators are given more priority than assignment operators, in the sense that a calculation is performed first and only then is the result assigned to a variable. The ranking also includes, for example, the well-known associative law (dot before dash rule) of mathematics:




10
14
#include <stdio.h>

int main(){
  printf("%d\n",  4 + 3  * 2);
  printf("%d\n", (4 + 3) * 2);
  return 0;
}

The multiplication operator has a higher precedence than the addition operator . Thus, in the first line, the multiplication is performed before the addition, just as is usual in mathematics. However , bracketing has the highest priority and is always performed before all other operators, which is why addition is performed first on the second line and only then is multiplication performed.

Processing Direction

If several operators with the same rank occur one after the other, the so-called processing direction determines whether the operators are processed either from left to right ->> or from right to left <- . Operators with the same rank always have the same processing direction. A complete listing of the processing direction of all operators can be found below.

The addition, for example, is processed from left to right, so in the following example first aadded band then the result with c. The assignment, however, is processed from right to left, so in the following example the value cof the variable bis first assigned and then the value that is now bstored in is awritten to . As a result, all three variables contain the same content as c.

a + b + c;
a = b = c;

A processing direction is also specified for unary operators. This is basically from left to right. Only the two operators post-increment operator++ and post-decrement operator-- are processed from right to left. With unary operators that are processed from left to right, the operator is on the left and the operand is on the right. When processing from right to left, the operator is on the right and the operand is on the left. The following example lists the unary negative operator (from left to right) and the unary post-increment operator (from right to left)

-a
a++

The operator brackets() and the range operators have NO processing direction. This is because they are actually just pointers to the compiler as to where to find a variable, or how the order of processing was manually set by the programmer. A compiler cannot create assembly code for such operators, and therefore no processing sequence can be specified either, since there is nothing to be processed. The range operator:: is given a direction here on this page, since this operator can be used for nested ranges, which are addressed with this operator from left to right.

Rank 1

Rank 2   →

Rank 3   ←

Rank 4   →

Rank 5   →

Rank 6   →

Rank 7   →

Rank 8   →

Rank 9   →

Rank 10   →

Rank 11   →

Rank 12   →

Rank 13   →

Rank 14   ←

Rank 15   ←

Rank 16   ←

Remarks

It should be noted that such a listing is never explicitly defined in the standards. Rather, the order of precedence and the direction of processing are fixed purely on the basis of the syntax rules. However, like many other authors, the author feels that a list like the one above is didactically useful. However, different sources handle the conversion of the syntax rules into such a ranking differently, which is why dozens of such rankings exist, all saying roughly the same thing, but not exactly the same thing. However, a programmer will very rarely encounter problems when actually programming, since many operators do not even appear together and therefore a ranking is no longer necessary.

It should also be noted that the rules listed here specify the exact processing order of the operators, but not the evaluation of the operands required for this. In C and C++, it is only rarely specified when an operand is to be read or written within an expression. This gives compilers the opportunity to heavily optimize the code. However, if operands in the same expression are read and written at the same time, this can lead to undefined behavior in very specific cases, since each compiler could read or write the operands in a different order. More information can be found in the sequence points .