lundi 29 avril 2019

GCC interprets uint8_t and uint16_t as signed?

My test code:

#include <cstdint>
#include <cstdio>

int main() {
    const constexpr uint8_t x = 64;
    printf("%u", x);
}

Here is how I compiled with GCC 8.2:

g++ -Wall test_format.cpp -o test_format -O3 -std=c++17 -Wformat-signedness

And here is GCC's output:

test_format.cpp: In function ‘int main()’:
test_format.cpp:6:9: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int’ [-Wformat=]
  printf("%u", x);
         ^~~~

Tho, if I try to print an uint32_t, it has no error/warning.

I wonder why GCC expects uint8_t to be signed int.

Thanks.

Aucun commentaire:

Enregistrer un commentaire