【发布时间】:2019-09-18 16:26:02
【问题描述】:
我的测试代码:
#include <cstdint>
#include <cstdio>
int main() {
const constexpr uint8_t x = 64;
printf("%u", x);
}
这是我使用 GCC 8.2 编译的方式:
g++ -Wall test_format.cpp -o test_format -O3 -std=c++17 -Wformat-signedness
这是 GCC 的输出:
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);
^~~~
但是,如果我尝试打印 uint32_t,它没有错误/警告。
我想知道为什么 GCC 期望 uint8_t 被签名为 int。
谢谢。
【问题讨论】: