gpt4 book ai didi

c - 如果 puts() 函数没有遇到空字符会怎样?

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:25 27 4
gpt4 key购买 nike

考虑以下程序。

#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 标准对此有何规定?

最佳答案

puts 将结束读取 dont 的最后一个元素,这是未定义的行为。

所以不,你不能保证每次都有相同的输出。就此而言,您根本无法保证任何输出 - 您无法保证任何,因为这是未定义的行为。

关于c - 如果 puts() 函数没有遇到空字符会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30681297/

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