C Language: Arithmetic operators

Source: Internet
Author: User
Tags arithmetic operators

The arithmetic operator, by definition, is the symbol of the operation

+ addition operator

-subtraction operator or negative operator

* Multiplication operator

/Division Operator

%-modulo operator or call-to-rest operator requiring that both sides are integer data

Arithmetic expressions: A formula that uses arithmetic operators to drop data, called an arithmetic expression, such as A+b, 10/5, and so on, if the expression is complex, pay attention to the order of operations, and the order of operations is carried out according to the combination direction and priority of the operator.

Combination direction

Arithmetic operators are left-to-right in the direction of operation. For example, Expression 2+3+4, first calculate 2+3

Priority level

The higher the priority, the more the first combination, and the absence of a participating operation is simply the combination of the operator and the operand:

Negative operator (-) > Multiplication operator (*), Division operator (/), modulo operator (%) > addition operator (+), subtraction operator (-)

Parentheses

If you need to calculate a low priority, you can enclose it in parentheses, with the highest precedence of the parentheses

For example 1+2*5-3, the default order is *, +,-

If you want to prioritize addition, you should write (1+2) *5-3, and the final results are different.

Calculation Order

In the case of multiple operator blending operations, a high-priority operator is combined with its operand, called a whole, but not computed, and is evaluated by the outermost operator, which is computed in the direction of the outermost operator, and then a layer of computation.

For example, the expression 3*4+2*8/-4 is evaluated in the following order: first-combined with 4, then 3*4 combined, 5*8 combined, combined with the result (3*4) + ((2*8)/(-4)), at this time the outermost is +

The addition operation calculates the left operand first, then 3*4 the result operation to 12+ ((2*8)/(-4)), then calculates the + right operand ((2*8)/(-4))

Its outermost layer is/, then calculate the left 2*8 calculated result is 16/(-4), and then calculate 16/(-2) result is-4

Now the calculation expression is 12+ (-4), the final settlement result is 8

Note Points for arithmetic operations:

1. Automatic type conversionint a = 10.6;int B = 10.5 + 1.7;2. Automatic conversion of large type to small type, loss of precisionAutomatic type promotionint B = 10.5 + ten;3. Lift the 10 on the right to double typedouble b = 1.0/2;solve the problem of the precision of Division4. Forcing type conversionsDouble A = (double);double b = (double) (a);

C Language: Arithmetic operators

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.