对于学校项目,我要编写C函数printf.事情进展顺利,但有一个问题我找不到合适的答案,所以我在这里.
printf("PRINTF(d) \t: %d\n", -2147483648);
Run Code Online (Sandbox Code Playgroud)
告诉我(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
Run Code Online (Sandbox Code Playgroud)
但是如果我使用int变量,一切都很顺利:
int i;
i = -2147483648;
printf("%d", i);
Run Code Online (Sandbox Code Playgroud)
为什么?
我理解了许多观点,他们非常有趣.无论如何,我猜printf是使用<stdarg.h>librairy,所以,va_arg(va_list ap, type)也应该返回正确的类型.对于%d和%i,显然返回的类型是int.它有什么改变吗?