gpt4 book ai didi

c++ - 是否定义了打印 NULL 字符串?

转载 作者:太空狗 更新时间:2023-10-29 21:05:53 27 4
gpt4 key购买 nike

这段代码是否有定义的行为?

char *str = NULL;
printf("%s\n",str);

在 C++(98/03 和 11)和 C (99) 标准的上下文中。

最佳答案

C 中的未定义行为

(C99, 7.19.6.1p8) "s If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type."

对于某些编译器/libc,它会打印 (null),而对于其他一些,它会打印段错误。

(注意:在 C 中,违反不是约束的 shall 是未定义的行为,请参阅标准 C 中的 4.p2“一致性”)

关于c++ - 是否定义了打印 NULL 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com