【发布时间】:2021-12-27 19:07:39
【问题描述】:
#include <stdio.h>
void a(signed char a) {
printf("%u\n", a);
}
void b(short b) {
printf("%u\n", b);
}
void c(int c) {
printf("%u\n", c);
}
void d(long d) {
printf("%u\n", d);
}
void e(long long e) {
printf("%u\n", e);
}
int main() {
a(-1); //no warning
b(-1); //no warning
c(-1); //no warning
d(-1); //warning
e(-1); //warning
return 0;
}
使用gcc -std=c17 -pedantic -Wall -Wextra test.c 和g++ -std=c++17 -pedantic -Wall -Wextra test.cpp 使用gcc 11.2.0 编译和测试。两者都不会对a()、b() 和c() 发出任何警告。这是故意的,还是一个错误?
【问题讨论】:
-
您希望得到什么样的警告?您的问题中缺少该部分...
-
您期待什么警告?您实际上收到了哪些警告?不要假设每个人都在使用 gcc。
-
他们为什么要这样做?
-1是显示的每个函数的可接受参数值。 -
是的,但
%u仍然会与int值不匹配,无论是否提升。 gcc 对printf参数进行编译时验证。 -
@Remy Lebeau Re "%u 仍然会与 int 值不匹配",
-Wformat-signedness会发现这一点。