Understanding Modulus Operators in C Programming

In summary, the modulus operator in C is associated with integer division and gives the remainder when the first number is divided by the second number. When the first number is smaller than the second number, the result is equal to the first number. The behavior for mixed sign operands is compiler dependent, but it is often based on the sign of the dividend. Some older computers may produce "correct" results based on different algorithms.
  • #1
ming2194
32
0
im the very beginner of learning c programe so i would like to ask the follow questions and hope someone can give me some idea::

i know:

3%2 = 1

8%3 = 2

12%4 = 0

but I am not sure about the followings (or my concept is wrong or right):

12%100 = 12
1%2 = 1

can i say when the number after "%" is larger then the number before "%", the ans is equal to the formar number?

how about (-ve)%(+ve) or (+ve)%(-ve) or (-ve)%(-ve) ?

rly cannot get idea.

*it's not homework and it's just created by myself
 
Technology news on Phys.org
  • #2
ming2194 said:
im the very beginner of learning c programe so i would like to ask the follow questions and hope someone can give me some idea::

i know:

3%2 = 1

8%3 = 2

12%4 = 0

but I am not sure about the followings (or my concept is wrong or right):

12%100 = 12
1%2 = 1

can i say when the number after "%" is larger then the number before "%", the ans is equal to the formar number?

how about (-ve)%(+ve) or (+ve)%(-ve) or (-ve)%(-ve) ?

rly cannot get idea.

*it's not homework and it's just created by myself
Modulus operations are always done on integer types, and are most often done on positive integer types. The modulus operater is associated with integer division in C, which is a kind of division that gives whole-number quotients. For example, 6/2 == 3, which is not surprising, but 7/2 == 3 as well. It is only when one or both numbers in division are floating point numbers that we get a floating point result. E.g, 7.0/2 == 3.5, or 7/2.0 == 3.5.

In an expression a % b, the result is the remainder when a is divided by b. If a < b, then a % b == a, as you have discovered.

For your questions about positive and negative operands use in a modulus expression, I looked in a book I have had for a long time, "The C Programming Language," by Brian Kernighan and Dennis Ritchie. In it, they say "On all machines covered by this manual, the remainder has the same sign as the dividend."

The result of a modulus expression with operands that have mixed signs is compiler dependent. The documentation for Microsoft Visual C says this: "If either operand is negative and the result is inexact, the result is implementation defined." For example, it's possible that different compilers would produce different results for 7 % -2.
 
  • #3
One argument for a mathematically "correct" answer for mixed sign division and modulo is for the operation to be origin independent. (x+a*y)%(y) should always produce the same result regardless of the value of a or it's sign. As an example of this, imagine a "modulo 10" clock that cycles through the number 0 through 9 incrementing once each second (10 second cycle time); currently the display indicates 4, what will the display read 20 seconds from now (a = +2)? What did the display indicate 40 seconds ago (a = -4)?

If you plot a graph of integer division by some fixed divisor, you should get a stair step graph that doesn't have a double or skipped step at zero. APL is one of the few computer languages to get this right, a language that dates back to the 1960's.

The sign of the remainder should equal the sign of the divisor (or zero), despite the wording in the C reference. It makes more sense and it's more consistent for m%n to have |n| possible values (and not 2|n|-1 values).

Some examples of "correct" results, but it's unlikely you get these results from any modern computer (unless it's running a language that defines this behavior such as APL):

101/10 = +10
101%10 = +1

101/-10 = -11
101%-10 = -9

-101/10 = -11
-101%10 = +9

-101/-10 = -10
-101%-10 = -1

Using that last line, include a multiplier factor, as mentioned above:

(-101 + ( (a)*(-10) ) )%-10 = -1

a = +20 => (-101 + ( (+20)*(-10) ) )%-10 => -301%-10 = -1
a = -20 => (-101 + ( (-20)*(-10) ) )%-10 => +99%-10 = -1

As mentioned, this behavior for mixed signs isn't defined in C, and in most computers integer division won't follow this rule either. Some older computers that implemented division using one type of a "non-restoring" algorithm would get the "correct" results.
 
Last edited:

1. What is a modulus operator in C programming?

A modulus operator, denoted by the % symbol, is a mathematical operator used to find the remainder of a division calculation. It returns the remainder of the division of two operands.

2. How is the modulus operator used in C programming?

The modulus operator is most commonly used in programming to check if a number is even or odd. It can also be used to perform calculations involving cyclic patterns, such as calculating the day of the week based on a given number of days.

3. Can the modulus operator be used with non-integer operands?

No, the modulus operator can only be used with integer operands. If non-integer operands are used, the compiler will return an error.

4. What happens when a negative number is used with the modulus operator?

When a negative number is used with the modulus operator, the result will also be negative. For example, -5 % 2 will return -1 as the remainder.

5. Can the modulus operator be used with floating-point numbers?

No, the modulus operator can only be used with integer operands. If floating-point numbers are used, the compiler will return an error.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
873
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
974
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
22
Views
758
Back
Top