C Division operator "/" and division operator "%"

Source: Internet
Author: User

It seems like two very simple operators, but it is not easy to master and use them well. This article is a summary of all aspects of such operators, and I hope it will be helpful to you.

Division operator "/". Binary operators with left-bound attributes. When all the values involved in the calculation are integer, the result is integer and the decimal point is removed. If one of the computations is real, the result is double-precision real.

For example:

5/2 = 2, 1/2 = 0

5/2. 0 = 2.5


The remainder operator "%", a binary operator, with a left combination. The amount involved in the calculation isInteger. The result of the remainder operation is equalRemainder.

For example:

5% 2 = 1,1% 2 = 1

The result of 5% 2.0 and 5.0% 2 is a syntax error.

Turn: when the number of current faces is smaller than the following

In fact, the remainder operation can be viewed
A % B = a-(int) (a/B) * B

1% 2 = 1
2% 5 = 2

A % B
If a <B, the quotient is 0, and the remainder is.

Ha, this relational expression a % B = a-(int) (a/B) * B is explained in this way.
First calculate (a/B), then a-(a/B value), and then multiply by B
Example 1.
100 divided by 2 = 50
If the result is an integer, the value is 0 (the reason is that the value of 100 divided by 2 is an integer, not 50 ............. )
Example 2.
9 divided by 2 = 4.5
Then, multiply 4 by 2 = 8.
The result of 9-8 is the remainder.

In C, what are the values of-3/16, 16/-3,-3%, and 16%-3?

The difference between the positive and negative signs is the same as the General Arithmetic. The symbols are the same as positive signs, and the differences are negative.
The positive and negative trade of the remainder symbol is the same as that of the divisor.
-3/16 = 0 16/-3 =-5-3% 16 =-3 16%-3 = 1

 

FM: http://blog.163.com/lvjin658101@yeah/blog/static/162888047201121582012546/

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.