Understanding Modulus Operators in C Programming

  • Thread starter Thread starter ming2194
  • Start date Start date
  • Tags Tags
    Modulus Operators
AI Thread Summary
The discussion centers on understanding the modulus operator (%) in C programming, particularly for beginners. The modulus operation returns the remainder of a division. Key points include that when the divisor is larger than the dividend, the result equals the dividend (e.g., 12 % 100 = 12 and 1 % 2 = 1). The behavior of modulus with negative and positive integers is less straightforward and can vary by compiler. Generally, the result of a modulus operation takes the sign of the dividend, but this can differ between implementations, especially with mixed signs. The discussion references "The C Programming Language" by Kernighan and Ritchie, which notes that the remainder's sign aligns with the dividend. Examples illustrate how different combinations of positive and negative operands yield varying results, emphasizing that behavior is not consistently defined in C, leading to potential discrepancies across different compilers.
ming2194
Messages
31
Reaction score
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
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.
 
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
23
Views
2K
Replies
5
Views
3K
Replies
1
Views
4K
Replies
4
Views
1K
Replies
1
Views
440
Replies
1
Views
1K
Replies
19
Views
1K
Back
Top