如果类型不符合类型说明符,printf如何工作?

Old*_*ool 1 c printf

int main()
{
printf("%c", "\n");

return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里根据类型说明符,需要一个字符.但我们正在通过它const char *.我希望它会在代码块GNU GCC编译器中给我一个警告信息,但它没有给我任何警告和打印$字符.

为什么它没有给出任何类型的警告?

And*_*own 6

您需要启用该警告.

编译你的代码test.cpp,并添加#include <stdio.h>printf与海湾合作委员会下4.7.2正确的警告标志:

$ gcc -c -Wformat test.cpp
test.cpp: In function 'int main()':
test.cpp:5:18: warning: format '%c' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat]
Run Code Online (Sandbox Code Playgroud)