最佳答案
对于一个学校项目,我必须编写 C 函数 printf。一切都很顺利,但有一个问题我找不到一个好的答案,所以我来了。
printf("PRINTF(d) \t: %d\n", -2147483648);
告诉我(gcc -Werror -Wextra -Wall) :
error: format specifies type 'int' but the argument has type 'long'
[-Werror,-Wformat]
printf("PRINTF(d) \t: %d\n", -2147483648);
~~ ^~~~~~~~~~~
%ld
但是如果我使用一个 int 变量,一切都会很顺利:
int i;
i = -2147483648;
printf("%d", i);
为什么?
我理解了很多观点,它们非常有趣。无论如何,我想 printf正在使用 <stdarg.h>库,因此,va_arg(va_list ap, type)也应该返回正确的类型。对于 %d和 %i,返回的类型显然是 int。这能改变什么吗?