title: uint8_t not rollover to 0 after reaching 255 not working properly
body: <p>I am new to the <strong>C-Headers</strong> - <code>stdint.h</code> and <code>inttypes.h</code>. I was trying out some code to get an inkling of how <code>uint8_t</code> works. But, it seems to have encountered a problem.</p>
  
  <p>I have declared 4 <code>uint8_t</code> integers with the boundary values 0, 255, 256, -1 respectively and performed some simple arithmetic operations on it. I did this since I wanted to know what <strong>errors/warnings</strong> does the c-compiler (I am using <code>gcc 5.4.0</code> on linux) generate. And if not, I was interested in knowing what the output looked like. The code in given below.</p>
  
  <pre><code>#include &lt;inttypes.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  
  int main() {
          int a = 10;
          printf("%d\n", a++);
          printf("%d\n\n", a);
  
          // 8 bit unsigned integer -&gt; range[0, 255]
          uint8_t ua81=0, ua82=255, ua83=256, ua84=-1;
          printf("--------STDINT.H----uint8_t--DEMO--------\nua81 = %" PRIu8 "\n", ua81);
          printf("ua82 = %" PRIu8 "\nua83 = %" PRIu8 "\nua84 = %" PRIu8 "\n\n", ua82, ua83, ua84);
          printf("ua81+1 = %" PRIu8 "\nua82-3 = %" PRIu8 "\nua83-4+7 = %" PRIu8 "\nua84-1+20 = %" PRIu8 "\n----------\n\n", ua81+1, ua82-3, ua83-4+7, ua84-1+20);
  
          return 0;
  }
  </code></pre>
  
  <p>The output of this code is as follows:</p>
  
  <pre><code>vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ vi stdint_h.c
  vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ gcc -Wall stdint_h.c -o a
  stdint_h.c: In function ‘main’:
  stdint_h.c:11:33: warning: large integer implicitly truncated to unsigned type [-Woverflow]
    uint8_t ua81=0, ua82=255, ua83=256, ua84=-1;
                                   ^
  vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ ./a
  10
  11
  
  --------STDINT.H----uint8_t--DEMO--------
  ua81 = 0
  ua82 = 255
  ua83 = 0
  ua84 = 255
  
  ua81+1 = 1
  ua82-3 = 252
  ua83-4+7 = 3
  ua84-1+20 = 274
  ----------
  </code></pre>
  
  <p>As mentioned earlier, I am using a Linux machine for this with the <code>gcc 5.4.0</code> compiler.</p>
  
  <pre><code>vagrant@ubuntu-xenial:~/Documents/Coding Practice/p_c$ gcc --version
  gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
  Copyright (C) 2015 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  </code></pre>
  
  <p>I am not able to understand why, for the variable <code>ua84</code>, the value is not rolling over to <code>0</code> after reaching <code>255</code>, while the same is working for <code>ua83</code>. I think the value, considering the rollover after 255, of <code>ua84</code> should have been <code>18</code>.</p>
  
  <p>I feel this has got something to do with the <strong>Implicit Truncation</strong> mentioned in the <code>warning</code> that was generated pertaining to the variable <code>ua83</code> while compiling. I don't know exactly what is wrong with that. I would also like to know how to go about that I still want to continue using <code>uint8_t</code>.</p>