Operator, and a priority

C ++: Operator Priority and

Qiang 201831061427

Introduction: In writing the code, the use of essential operator, +, like when we used to learn elementary mathematics -, *, /, which is four mathematical operators, we have learned formulas: first multiplication and division, addition and subtraction after, it is the priority of the four operations, and statements in the code, so many operators, is not the priority it should have?

table of Contents 

  I. operator
  two Priority
  III. Data Type Conversion

A. Operators

   There are a wide range of operators for operation in C ++, the
:: [] -> + -.! ~ - + * -> and so many operators in our program for writing the statement, the operator essential.

II. Priority

  In C ++, an expression may contain a plurality of different operators connected together, the data having different types of data objects; a different binding sequence may have different results even wrong operation error occurs because when the expression contains a variety of when the operation must be combined in a certain order, in order to ensure the reasonableness and accuracy of the results, the uniqueness of operations. And this certain order, called operator precedence.

C ++ operator precedence

E.g:

#include <iostream>
using namespace std;
main()
{
 int a = 21;
 int b = 10;
 int c ;
 c = a + b;
 cout << "Line 1 - c 的值是 " << c << endl ;
 c = a - b;
 cout << "Line 2 - c 的值是 " << c << endl ;
 c = a * b;
 cout << "Line 3 - c 的值是 " << c << endl ;
 c = a / b;
 cout << "Line 4 - c 的值是 " << c << endl ;
 c = a % b;
 cout << "Line 5 - c 的值是 " << c << endl ;
 c = a++;
 cout << "Line 6 - c 的值是 " << c << endl ;
 c = a--;
 cout << "Line 7 - c 的值是 " << c << endl ;
 return 0;
}

Ask someone to enter their own code to test, learn and understand operator precedence.
result:

Line 1 - c 的值是 31
Line 2 - c 的值是 11
Line 3 - c 的值是 210
Line 4 - c 的值是 2
Line 5 - c 的值是 1
Line 6 - c 的值是 21
Line 7 - c 的值是 22

III. Data type conversion

  When a plurality of different types of data occur simultaneously performing operations, data type conversion usually occurs, and the data type conversion is typically performed automatically divided implicit conversion and executed by the programmer cast (explicit conversion).

Implicit conversion

  In order to ensure the consistency and reliability of the system data, if the same operation involved in two types of operands, the operating system will automatically convert data. The basic principle of the conversion is to convert the data type low-high type data .

Cast

  Cast contains six types of transfer syntax:
  1. The type specifier (expression)
  2. (type specifier) expression
  3.const_cast <type specifier> (expression)
  4.dybamic_cast <type specifier> (Expression )
  5.reinterpret_cast <type specifier> (expression)
  6.static_cast <type specifier> (expression)

Guess you like

Origin www.cnblogs.com/Drac/p/11517835.html
Recommended