C + + arithmetic operators and arithmetic expressions

Source: Internet
Author: User

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.

Common arithmetic operators
operator Description Example
+ addition operator, or positive value operator 3+5,+3
- Subtraction operator, or negative operator 5-2,-3
* Multiplication operator 3*5
/ Division operator 5/3
% Modulo operator, or call remainder operator Both sides should be integer data, such as the value of 7%4 3


It should be explained that the result of dividing two integers is an integer, such as 5/3 with a result value of 1, and a fractional portion. However, if there is a negative value in the divisor or divisor, the rounding direction is not fixed. For example, -5/3 gets the result on some C + + systems-1, and some C + + systems give the result-2. Most compiled systems take the "rounding to 0" method, that is, the value of 5/3 equals the value of 1,-5/3 equals-1, and the rounding to 0 draws closer.

If you participate in the +,-, *,/operation of two numbers with a number of float type data, the result of the operation is double type, because C + + at the time of operation for all float type data is processed by double type.

Precedence and binding of arithmetic expressions and operators

A formula that conforms to C + + syntax rules, called C + + arithmetic expressions, using arithmetic operators and parentheses to concatenate operands (also called operands). Operands include constants, variables, functions, and so on. For example, here is a valid C + + arithmetic expression:
a*b/c-1.5+ ' a '

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

C + + Specifies the binding direction of the various operators (binding), the arithmetic operators are bound to "from left to right", that is, first left and right, so B first with a minus sign, perform the operation of a-a, and then perform the operation plus C. "From left to right in the direction of the Union" is also known as "left-associative", that is, the operand first with the operator on the left. You can see later that some operators have a union direction of "right-to-left", that is, right-associative (for example, assignment operators). The concept of "bonding" is not in some other high-level languages, it is one of the features of C and C + +, and I hope to be able to figure it out.

For all operators in C + + and their precedence and binding, see here: C + + operator precedence table

The mixed operation between various kinds of numerical data in expressions

You often encounter different types of data in an expression, such as:
"A ' +1.5-8765.1234* ' B '
When the operation is performed, the different types of data are converted to the same type first, and then the operation is performed. The rules for the conversion are shown in Figure 2.7.


Figure 2.7


Suppose I is specified as an integer variable, f is a float variable, d is a double variable, and E is a long type, with the following expression:
"A ' +i*f-d/e
The order of operations is:

    1. For the operation of the "a", we first convert ' a ' to an integer 97 and the result is 107.
    2. Perform the i*f operation. First, I and F are converted to double type, the result of the operation is double type.
    3. Integer 107 is added to the product of the i*f. The integer 107 is converted to a double precision number (after the decimal point plus a number of 0, that is, 107.000 ... 00), the result is a double type.
    4. The variable e is converted to a double type, and the d/e result is a double type.
    5. The result of the +i*f ' a ' is subtracted from the quotient of the d/e, and the result is a double type.


The type conversions described above are performed automatically by the system.

C + + arithmetic operators and arithmetic expressions

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.