【发布时间】:2015-08-21 06:37:03
【问题描述】:
考虑以下程序。
#include <stdio.h>
int main(void)
{
char side_a[] = "Side A";
char dont[] = {'W', 'O', 'W', '!' };
char side_b[] = "Side B";
puts(dont); /* dont is not a string */
return 0;
}
我知道 puts() 函数在遇到空字符时会停止。但是在上面的程序中我没有指定空字符。那么什么时候停止打印呢?这个程序是否调用了未定义的行为?是否保证在各种 C 编译器上为该程序获得相同的输出? C 标准对此有何规定?
【问题讨论】:
-
在 C 中,一个有效的字符串是空终止的,所有的 lib API 都期望空终止的字符串