printf()当我们在没有格式说明符的情况下向它提供多个参数时,它的行为是什么?
例子:
int main()
{
printf("hello", "hi");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器在编译上述程序时会产生警告?:
warning: too many arguments for format [-Wformat-extra-args]
Run Code Online (Sandbox Code Playgroud)
如果我们编译如下类似的程序:
int main()
{
char *s1 = "hello";
char *s2 = "hi";
printf(s1, s2);
}
Run Code Online (Sandbox Code Playgroud)
不产生警告。这是什么原因?
另外,为什么两个程序hello都只输出而不打印hi?
int main()
{
char ch1 = 128;
unsigned char ch2 = 128;
printf("%d\n", (int)ch1);
printf("%d\n", (int)ch2);
}
Run Code Online (Sandbox Code Playgroud)
第一个 printf 语句输出 -128 和第二个 128。据我说,ch1 和 ch2 将具有相同的二进制表示存储的数字:10000000。所以当我将两个值都类型转换为整数时,它们最终是如何不同的值?