title: The mod of negative numbers in C
body: <p>In C the following code ...</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdbool.h&gt;
  #include&lt;stdlib.h&gt;
  
  int main() {
     int result = (-12) % (10);
     printf("-12 mod 10 = %d\n",result);
     return 0;
  }
  </code></pre>
  
  <p>gives this output</p>
  
  <pre><code>&gt; gcc modTest.c
  &gt; ./a.out
  -12 mod 10 = -2
  </code></pre>
  
  <p>But according to <a href="http://www.miniwebtool.com/modulo-calculator/?number1=-12&amp;number2=10" rel="nofollow">this mod calculator</a> -12 mod 10 = 8</p>
  
  <p>In Python ...</p>
  
  <pre><code>&gt; python
  Python 3.3.0 (default, Mar 26 2013, 09:56:30)
  [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; (-12) % (10)
  8
  </code></pre>
  
  <p>What is going on in C that produces -2 instead of 8?</p>