Let*_*_Be 3 c c++ standards undefined-behavior
这段代码有定义的行为吗?
char *str = NULL;
printf("%s\n",str);
Run Code Online (Sandbox Code Playgroud)
在 C++(98/03 和 11)和 C (99) 标准的上下文中。
C 中未定义的行为
(C99, 7.19.6.1p8) “s 如果不存在 l 长度修饰符,则参数应是指向字符类型数组的初始元素的指针。”
对于某些编译器/libc,它会打印(null),而对于其他一些编译器,它会出现段错误。
(注意:在 C 中,违反shall非约束的 a 是未定义行为,请参阅标准 C 中的 4.p2“一致性”)