# Thread: Help understanding the modulus operator

1. ## Help understanding the modulus operator

Hello,

I'm having difficulty understanding how the modulus operator works. For instance, I have a piece of code:

Code:
```#include <stdio.h>

int main(void)
{
int i;

for (i=0; i<8; i++)
printf("index_1[%d] = %d\n", i, (i % 2));

for (i=0; i<8; i++)
printf("index_2[%d] = %d\n", i, (i % 8));

return 0;
}```
I understand how I come to the remainder of 1 for the results listed for index_1:

index_1[0] = 0
index_1[1] = 1
index_1[2] = 0
index_1[3] = 1
index_1[4] = 0
index_1[5] = 1
index_1[6] = 0
index_1[7] = 1

But I don't understand how I am coming to the results listed for index_2:

index_2[0] = 0
index_2[1] = 1
index_2[2] = 2
index_2[3] = 3
index_2[4] = 4
index_2[5] = 5
index_2[6] = 6
index_2[7] = 7

For example, 4 % 8 doesn't have a remainder because the result of the division is 0.5. Likewise for 1 % 8, the result of the division is 0.125. How is it arriving at these numbers? Is it possibly due to using an int and its rounding?

Any help would be appreciated. Thank you.

2. 1 % 5 = ( 1 / 5 = 0 R 1 )

Quzah.

3. Ok, I think I might be following. Since 5 cannot go into 1 any number of times (0 times), the remainder is equal to the dividend? I'm arriving at this logic based on the answers I received in my previous example:

index_2[0] = 0
index_2[1] = 1
index_2[2] = 2
index_2[3] = 3
index_2[4] = 4
index_2[5] = 5
index_2[6] = 6
index_2[7] = 7

If I re-write it using what I believe to be the correct understanding, then I arrive at:

0 % 8 = 0
1 % 8 = 1 (8 cannot go into 1 any number of times, therefore the remainder is equal to the dividend)
2 % 8 = 2 (8 cannot go into 1 any number of times, therefore the remainder is equal to the dividend)
etc......

Is this way of thinking of the issue correct? Thank you for the reply

4. Yep. So 17 % 8 = 1.

Quzah.

5. Originally Posted by matrixx333
For example, 4 % 8 doesn't have a remainder because the result of the division is 0.5. Likewise for 1 % 8, the result of the division is 0.125. How is it arriving at these numbers? Is it possibly due to using an int and its rounding?
If everything is being done with ints in C, the compiler will not quietly introduce a floating point calculation without you knowing.

The result of the % is an int. 1 % 8 is 1. The % operator does not work for non-integral types.

Mathematically a % b is equivalent to a - (a/b)*b. You are assuming there is some magically real (or floating point - computers don't work with true real values) variable in play. There is not.

The a/b is integer division which (assuming a and b are positive) means rounding towards zero. So 1/4 is 0, 2/4 is 0, 3/4 is zero, 4/4 is 1, 5/4 is 1, etc. According 1%4 is 1 - 0*4 = 1, 2%4 is 2-0*4 = 2 , 3%4 is 3 - 0*4 = 3, 4%4 is 4 - 1*4 = 0, 5%4 is 5 - 1*4 = 1, .....

For negative values, the logic is a little different (the rule for integer division with negative values varies with version of the C standard) but I won't bore you with that.

6. The 17 % 8 = 1 example is easier for me to understand because 8 actually goes into 17 two times and leaves a remainder of 1. That's how I was able to understand the index_1 case I listed. 3 % 2 = 1 because 2 goes into 3 one time with a remainder of 1.

I was having a hard time with the fractional examples because I didn't understand how the program was arriving at the remainder. Thank you both for your help.

You provided the exact answer I was looking for grumpy, thanks again.