<分区>
对于一个学校项目,我必须编写 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
.它会改变什么吗?