1

While debugging my C code I've noticed that the modulo operator does not calculate the right results. I've separated the code into a single file, but no success... What's going on here?

My system:

$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
$ uname -s -r
Linux 3.10.0-957.12.2.el7.x86_64
$ uname -v
#1 SMP Tue May 14 21:24:32 UTC 2019
$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
#include <stdio.h>
int main() {
    int i;
    printf("[");
    for(i=-11; i<=11; i++) {
        printf("%d ", i%11);
    }
    printf("]\n");
    return 0;
}

Compile and run:

gcc -Wall -Wextra -o test_modulo test_modulo.c
./test_modulo
[0 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 0 ]

The modulo-function in the python-console:

>>> print([i % 11 for i in range(-11,11)])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
curiousguy
  • 8,038
  • 2
  • 40
  • 58
Chris
  • 310
  • 4
  • 14

0 Answers0