3

I tried to read several unsigned char values through scanf and found some strange bug. During the second call of scanf, first unsigned char variable gets overridden with 0. But writing scanf(" %hhu", &second); will cause override of the second unsigned char variable.

#include <stdio.h>

int main(void){
    unsigned char first, second;
    printf("Type first unsigned char variable: ");
    scanf("%hhu", &first);
    printf("first = %hhu\n", first);
    printf("Type second unsigned char variable: ");
    scanf("%hhu", &second);
    printf("first = %hhu\n", first);
    printf("second = %hhu\n", second);
    return 0;
}

Compiler details:

gcc-core (gcc-5.1.0-tdm-1-core)

binutils (binutils-2.24-1-mingw32-bin)

mingwrt (mingwrt-3.20-2-mingw32-dev, mingwrt-3.20-2-mingw32-dll)

w32api (w32api-3.17-2-mingw32-dev)

P.S. Using %hu or %u makes no difference.

M.M
  • 138,810
  • 21
  • 208
  • 365
Mr. White
  • 51
  • 5
  • 1
    Could you please specify compiler version you have used and also parameters you passed to it, like optimization level etc. – j2ko Feb 18 '19 at 21:48
  • 1
    It's most likely that second follows in address from first. So %hhu may be being interpreted as %hu. hhu is a C99 extension thus the question from j2ko about compiler version. – carveone Feb 18 '19 at 21:54
  • It's just mingw gnu gcc compiler with no special inputs – Mr. White Feb 18 '19 at 21:56
  • 2
    MinGW uses the Microsoft library, and the Microsoft C library is probably still C90 rather than C99 or later, so it probably doesn't handle the `hh` size modifier correctly. – Jonathan Leffler Feb 18 '19 at 21:58
  • Please edit that information into the question, and then delete the comment. – Jonathan Leffler Feb 18 '19 at 22:00
  • 1
    I can confirm that the Microsoft implementation of format functions does not recognizer `%hhu`, but I'm not in front of a Windows installation, so I wouldn't be able to test. You could try to write 65535 in `second` and see if it prints back 255 for both variables. If so, the conclusion is that you can't use `scanf` (or any variant thereof) to read `unsigned char` values on Windows. – zneak Feb 18 '19 at 22:29
  • zneak, it works exactly like you said! – Mr. White Feb 18 '19 at 22:33
  • use [mingw-w64](https://mingw-w64.org/) instead of whatever weird old mingw buggy version you have – M.M Feb 18 '19 at 23:33
  • @zneak *on this particular implementation, not Windows – M.M Feb 18 '19 at 23:35
  • @M.M, I said the Microsoft implementation. – zneak Feb 18 '19 at 23:37
  • @zneak you said "the conclusion is that you can't [do this] on Windows", when in fact the conclusion is that you can't do this on particular implementations. – M.M Feb 18 '19 at 23:44

1 Answers1

2

You appear to have an old, buggy version of MinGW.

Instead you can use mingw-w64. The project was forked in part due to the fact that the original project was unwilling to fix bugs like this, preferring to blame other parties.

Once using that compiler, use -D__USE_MINGW_ANSI_STDIO compiler switch to get conforming mode (it still defaults to Microsoft compatibility mode unfortunately).

M.M
  • 138,810
  • 21
  • 208
  • 365