Detailed notes on C + + operators

C + + operators

1, Arithmetic operator

Unary operators (monocular) have the highest priority, followed by multiplication, division and remainder, and finally addition and subtraction. And it has left associativity, that is, when the priorities are the same, it is calculated from left to right.

  1. Monocular: + or - (unary positive / negative value operation, for example: + 5 / - 5)
  2. Binocular 1: * or / or%
  3. Binocular 2: + or-

Note: the rules of some operators are as follows:

  1. The result of integer arithmetic operation is still an integer, for example, 21 / 6 = = 3, and the remainder is discarded
  2. For% remainder, both numbers are required to be integers, and the remainder of the dividend divided by the divisor is calculated. (if there is a negative number, the sign of the operation result and the dividend is the same, as follows) 21% - 4 = = 1- 21%4==-1

If the data types of the operation objects in the expression are different, the compiler will automatically perform type conversion, that is, implicit type conversion. The rules are as follows:

  1. Conversion from small data type to large data type. (let i be int, u be unsigned int, f be float, d be double,) 'a'+10+u+d-i/f

    In this formula, I / F (priority order) is calculated first to obtain a float type T1. Then calculate 'a' + 10 to get an int t2. Then calculate t2+u to get an unsigned int type t3. Then calculate t3+d to get a double T4. Then calculate t4-t1, get a final variable of double type, and output this variable.

  2. Conversion rule: char - > int - > unsigned int - > long - > float - > double

  3. First, the priority is used to judge the calculation order, and then the final value is solved by the conversion rule

unsigned int is used to represent unsigned integer (both represent values greater than or equal to 0).
And int / long / long and long / short are signed integers.

2, Assignment operator (=)

The left side of the assignor must be an lvalue that supports write operations. The arithmetic expression must be an R-value. Assign this value to the left

int i=0,j=i;  //Initialize and assign value to J
i+j=10;  //Error statement: arithmetic expression must be to the right of assignment character

If the types of the objects on the left and right sides of the assignor are different, the compiler will automatically convert the values of the objects on the right side to the left side, and the types are the same as those on the left side.

int i=0;
double d=3.14159;
cout<<i;  //The result is 3
cout<<d;  //The result was 3.14159
//This operation assigns the right value to the left, and the left type remains unchanged. The right value and the right type are always the same in this operation.

The assignment operation is right associative, as follows

i=j=5;  //Assign 5 to j first, and then assign j=5 to i

Since the priority of the assignor is relatively low, it usually needs parentheses to execute correctly.

i=2+j=4;  //Wrong expression. 2+j is an operation expression and cannot be used as the left object of the right assignment character
i=2+(j=4);  //Correctly expressed, the value of j=4 plus 2 is assigned to i, and finally j=4,i=6

3, Self increasing and self decreasing operators

In order to simplify operators such as i+=1 or i-=1, c + + provides + + and – operators.

int i=0,j;
j=i++;  //Operate first, then++
j=++i;  //(+ + +) before operation

Different forms of interpretation

int i=0,j;
j=i++;  //Equivalent to (j=i) and then (i++/i+=1), the result is j=0,i=1
j=++i;  //Equivalent to (j=(i+=1)), the result is j=2,i=2

4, Logical and relational operators


  1. Logical not (!)
  2. Less than (<)
  3. Less than or equal to (< =)
  4. Greater than (>)
  5. Greater than or equal to (> =)
  6. Equal to (= =)
  7. Not equal to (! =)
  8. Logical and (& &)
  9. Logical or(| |)

Expression construction

//This expression does not hold. According to the left combination, calculate (I < = J) first, and the result is 1 / 0. So as long as k is greater than or equal to 1, the formula on the right can be established.
i<=j  &&j<=k;  //Correct expression

Short-circuit evaluation

because&&and||Operators are all left-hand combinations,Therefore, there are the following rules:
  1. For & &, the value of the right object is calculated only when the left object is true

  2. For | |, the value of the right object is calculated only when the left object is false

     int i=1,j=2;
     bool b=!i && ++j;
     //Results, b=0,j=2
     //Reason: according to the priority, the order of expressions is: (! i) & & (+ + J), because! The value of i is false (0). In & &, it is evaluated according to the short circuit. The left side is false. The program ends directly without calculating the expression on the right side.

5, Comma operator

The evaluation method of comma expression is to calculate each arithmetic object from left to right, and the result is the rightmost object.

int i,j;
// In parentheses, calculate j=3 first, then j+=6 (j=9), and finally 5 + 6 = 11. Finally, 11 is assigned to i.
therefore i=11,j=9;

Among all operators, comma operator has the lowest priority (comma < assignment < logic < relationship < arithmetic)

6, Conditional operator (similar to branch statement)

**The priority of conditional operators is higher than assignment, less than logic and less than relation**
The conditional operator (?:) is a unique ternary operator in the following format:

//cond is a judgment statement. If true, enter expr1 and return the value of expr1; If false, enter expr2 and return the value of expr2

For example: assign the maximum value of the three objects to max

int a=4,b=5,c=6,max;
max=a>b? (a>c?a:c):(b>c?b:c);

The above code is a nested conditional operator, which is relatively complex. According to the priority, first enter the conditional statement to judge whether a > b? If it is true, enter (a > C? A: C); If false, enter (b > C? B: C). Because a < B, it enters false, and then a conditional operator. Because B < C, it gets C (false condition). So the result of the largest conditional operator is c(6), and finally 6 is assigned to max. End of statement.

Simple example:

int i = 10;
double j = 3.111333;
i = j;
i == 3 ? cout << "Yes" << endl : cout << "NO" << endl;
//The output result is "Yes"

7, Bitwise operator

The object of bit operator operation is an integer, which is used to process binary numbers. The operation process will not change the value of the operation object itself, and the result of the operation is an R-value. Bit operators are classified as follows:

  1. ~(reverse by position)
  2. < < (left shift)
  3. (move right)

  4. &(bit and)
  5. |(bit or)
  6. ^(bitwise exclusive or)

Where ~ is a unary operator, and the rest are binary operators.

8, Evaluation order

In C + +, only the operator evaluation order is specified:

Post auto increment / Auto decrement > pre auto increment / Auto decrement > logical not > arithmetic > relationship > Logic (and or) > condition (Branch) > assignment > comma

Ambiguity may occur under special circumstances

int i=0,j;
j=i*2+ ++i;
//Priority (+ + I) > (I * 2) > (+), so the result of operation is j = 3 (in VS environment)
//However, in other environments, it may occur that (i*2) is calculated first and then (+ + i), and the result may be j=1

Tip: don't read and write the same object in the expression!!!

Tags: C++

Posted by q1234ask on Fri, 02 Sep 2022 21:39:06 +0300