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.