c - Why is scanf("%hhu", char*) overwriting other variables when they are local? -
the title says all. i'm using gcc 4.7.1 (bundled codeblocks) , faced strange issue. consider this:
int main() { unsigned char = 0, b = 0, c = 0; scanf("%hhu", &a); printf("a = %hhu, b = %hhu, c = %hhu\n", a, b, c); scanf("%hhu", &b); printf("a = %hhu, b = %hhu, c = %hhu\n", a, b, c); scanf("%hhu", &c); printf("a = %hhu, b = %hhu, c = %hhu\n", a, b, c); return 0; }
for inputs 1, 2 , 3, outputs
a = 1, b = 0, c = 0 = 0, b = 2, c = 0 = 0, b = 0, c = 3
if i, however, declare a, b , c global variables, works expected. why happenning?
thank in advance
other details:
i'm running windows 8 64 bits. tried -std=c99 , problem persists.
further research
testing code
void printarray(unsigned char *a, int n) { while(n--) printf("%hhu ", *(a++)); printf("\n"); } int main() { unsigned char array[8]; memset(array, 255, 8); printarray(array, 8); scanf("%hhu", array); printarray(array, 8); return 0; }
shows scanf interpreting "%hhu" "%u". directly ignoring "hh". output of code input 1 is:
255 255 255 255 255 255 255 255 1 0 0 0 255 255 255 255
the important detail you're using windows, , presumably outdated or non-conforming c environment (compiler , standard library). msvcrt supports c89 (and then, not entirely correctly); in particular, there no "hh" modifier in c89, , it's interpreting "hh" same "h" (i.e. short
).
Comments
Post a Comment