scanf read integer input into uint8_t
<p>Is there a way to use scanf to read an unsigned char decimal input into a uint8_t variable?</p>

<p>I am afraid if I read %hu or %u into a uint8_t, it may corrupt the adjacent memory because uint8_t is one byte, but %hu is 2 bytes, and %u is 4 bytes.</p>

<p>I am using MinGW32</p>

<pre><code>gcc.exe (GCC) 4.9.3
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>The code I am worried about:</p>

<pre><code>/* worried.c - issue demo code. */
#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;
#include &lt;inttypes.h&gt;

int main(int argc, char **argv)
{
    uint8_t filler1 = 17;             /* do not corrupt me please. */
    uint8_t my_mem;
    uint8_t filler2 = 39;             /* do not corrupt me please. */ 

    printf("please enter a number: $ ");
    scanf("%u", &amp;my_mem);             /* corrupts fillers... */
    // scanf("%hu", &amp;my_mem);         /* this also corrupts... */
    // scanf("%hhu", &amp;my_mem);        /* still corrupts... */ 
    // scanf("%"SCNu8, &amp;my_mem);      /* still corrupts... */
    // scanf("%"PRIu8 "\n", &amp;my_mem); /* still corrupts... */

    printf("filler1 = %u.\n", filler1);
    printf("my_mem = %u.\n", my_mem);
    printf("filler2 = %u.\n", filler2);         

    return 0;
}
</code></pre>

<p>Note that the code above does corrupt the fillers, which is catastrophic when reading into a struct which is going to be directly written into a binary(record) file later.</p>

<p>I could solve it by casting from a temporary variable, but that takes a little extra work for my program which I was wondering if I could avoid forcing my program to do, and read into my_mem directly.</p>

<p>To date, it seems the solution that seems most likely is:</p>

<blockquote>
  <p>The only way to portably do this that works on GCC4.9.3 and newer is
  through casting. There are more elegant solutions on GCC5.2 e.g. %hhu,
  but they misbehave on GCC4.9.3.</p>
</blockquote>