Explain the use of operators in C + + programming _c language

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

C + + operators are very rich, making C + + operations very flexible and convenient. For example, the assignment number (=) is also treated as an operator, so that a=b=c=4 is a valid expression, which is different from other languages. C + + provides the following operators:
Arithmetic operators
+ (plus)-(minus) * (multiply)/(except)% (divisible balance) + + (self-reduction)
Relational operators
> (Greater than) < (less than) = = (equal to) >= (greater than or equal to) <= (less than or equal to)!= (not equal)
logical operators
&& (logic and) | | (Logical OR)! (Logical non)
Bitwise operators
<< (move left) >> (bitwise right) & (bitwise) | (bitwise OR) ^ (bitwise XOR) ~ (reverse by position)
Assignment operator (= and its extended assignment operator)
Conditional operator (?:)
Comma operator (,)
Pointer operator (*)
Reference operators and Address operators (&)
byte count operator (sizeof)
Coercion type conversion operator ((type) or type ())
Member operator (.)
operator pointing to member (->)
Subscript operator ([])
Other (such as function call operator ())

Basic arithmetic operators

In this chapter, we mainly introduce arithmetic operators and arithmetic expressions, assignment operators and assignment expressions, comma and comma expressions, and other operators will be introduced in later chapters.

It should be noted that the result of dividing two integers is an integer, such as the 5/3 result value of 1, the fractional part. However, if one of the divisor or divisor is negative, the direction of rounding is not fixed. For example, -5/3 has results on some C + + systems-1, and some C + + systems give results-2. Most of the compilation system adopts the method of "rounding 0", that is, the value of 5/3 equals 1,-5/3 is equal to 1, and the whole is rounded to 0.

If you participate in +,-, *,/the number of two digits of the operation is float, the result of the operation is double, because C + + is data-processing for all float data at the time of the operation.
Precedence and binding of arithmetic expressions and operators

An expression that conforms to the C + + syntax rules by using arithmetic operators and parentheses to connect an operation object (also known as an operand) to a C + + arithmetic equation. The operations object includes constants, variables, functions, and so on. For example, here is a valid C + + arithmetic expression:

  a*b/c-1.5+ ' a '

The C + + language prescribes the precedence and binding of operators. When solving an expression, it is performed in the order of precedence of the operators, such as multiplication and subtraction. If the left side of the expression a-b*c,b is a minus sign, the right side is multiplication, and the multiplication takes precedence over the minus sign, so it is equivalent to a A-(b*c). If the operators on either side of an operand have the same precedence, such as A-B+C, the specified "binding direction" is handled.

C + + stipulates the binding direction of various operators (binding), the combination of arithmetic operators is "from left to right", that is, first left and right, so B with the minus sign combined to perform a-b operations, and then perform the operation plus C. "The direction from left to right" is also called "left binding", that is, the Operation object is first combined with the operator on the left. You can see later that some operators are bound to "from right to left", that is, right binding (for example, assignment operators). The concept of "binding" is not available in other high-level languages, and is one of the features of C and C + +, hoping to find out.

The mixed operation of various numeric data in an expression

You often encounter operations between different types of data in an expression, such as:

  10+ ' A ' +1.5-8765.1234* ' B '

When an operation is performed, different types of data are first converted to the same type and then performed. The rules for the conversion are shown in the diagram.

Suppose I was given an integer variable, f is a float, D is a double, and E is a long, with the following expression:

  10+ ' A ' +i*f-d/e

The order of operations is:
For the operation of 10+ ' a ', first converts ' a ' to an integer 97, and the result of the operation is 107.
Perform a i*f operation. First, I and F are converted to double, and the result is double type.
The integer 107 is added to the product of the i*f. First, convert the integer 107 to a double-precision number (plus a number of 0 after the decimal point, or 107.000 ...). 00), the result is double type.
Converts the variable e to double, and the d/e result is a double type.
The result of 10+ ' a ' +i*f is subtracted from the quotient of the d/e, and the result is a double type.

The above type conversions are done automatically by the system.

C + + self-increasing and self-subtraction operators (--and + +)
in C and C + +, the self-add (+ +) and decrement (-) operators are used in expressions to increase the value of a variable by 1 or minus 1, such as:
++i (before using I, first make the value of I plus 1, if I's original value is 3, then execute J=++i, J's value is 4)
I (Before using I, the value of I is reduced by 1, if the original value of I is 3, then after the execution of J=--i, the value of J is 2)
i++ (after using I, add 1 to the value of I, if the original value of I is 3, then after performing j=i++, the value of J is 3, then I becomes 4)
i--(after using I, the value of I is reduced by 1, if the original value of I is 3, then after performing j=i--, the value of J is 3, then I becomes 2)
++i is the first to execute the i=i+1, then use the value of I, and i++ first use the value of I, then execute i=i+1.

Use + + and-----------------Make the program concise? Note that the
self-add operator (+ +) and the self-subtraction operator (--) are available only for variables, not constants or expressions.
+ + and--the combination direction is "from right to left". The
Self-increasing operators (+ +) and the self-subtraction operators (-) are very flexible, but in many cases they can be ambiguous and produce "unexpected" side effects. The
Self-add (subtract) operator is frequently seen in C + + programs and is commonly used in circular statements, which automatically add 1 to the loop variable. Also used for pointer variables, so that the pointer points to the next address.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.