C Language "%" operator

Source: Internet
Author: User

The operator "%" in C is the remainder operator, not the modulo operator. (The operator "%" is in C + +, Java, for the remainder operation, and in Python for modulo operations)

For a C language to take the remainder expression a% B, set its value to result, has the following formula:

1. k = (int) A/b ((int) coercion type conversion, the value is rounded in 0 direction)

2. result = A-k * b

The difference between the modulus and the remainder: when the value of K is calculated, the rounding direction is different, the modulo operator takes the result of a/b to negative infinity, and the take-up operator takes the result of a/b to 0 trade-offs. (for example, the value of K when -4/3 is modulo is-2, and the value of K at the time of taking the remainder is-1)

Example:

The value of 13% 4 is calculated.

1. k = (int) 13/4 = 3

2. Result = 13-3 * 4 = 1

Therefore, its value is 1.

Note: The two operands of the remainder operator must all be integers, and the second operand cannot be zero.

Special cases:

When |a| < |b|, the value is always a.

When |a| = |b|, the value is always 0.

When a = 0 o'clock, the value is always 0.

C Language "%" operator

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.