<分区>
这里有一段代码似乎没有错误地被接受:
#include <stdio.h>
#include <string.h>
int main() {
if (strcmp(1, 2))
printf(3);
}
使用 clang -std=c11 -Weverything
编译会产生 4 条警告:
badstrcmp.c:5:16: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
if (strcmp(1, 2))
^
/usr/include/string.h:77:25: note: passing argument to parameter '__s1' here
int strcmp(const char *__s1, const char *__s2);
^
badstrcmp.c:5:19: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
if (strcmp(1, 2))
^
/usr/include/string.h:77:43: note: passing argument to parameter '__s2' here
int strcmp(const char *__s1, const char *__s2);
^
badstrcmp.c:6:16: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
printf(3);
^
/usr/include/stdio.h:259:36: note: passing argument to parameter here
int printf(const char * __restrict, ...) __printflike(1, 2);
^
badstrcmp.c:6:16: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
printf(3);
^
badstrcmp.c:6:16: note: treat the string as an argument to avoid this
printf(3);
^
"%s",
4 warnings generated.
我的问题是为什么 C 标准允许它编译?。这种问题应该被诊断为错误,代码应该被拒绝。为什么 C 标准允许程序被翻译?