gpt4 book ai didi

c - gets() 和 scanf() 有什么区别

转载 作者:行者123 更新时间:2023-11-30 15:23:30 24 4
gpt4 key购买 nike

#include <stdio.h>

int main()
{
char text[100];
int length = 0;

gets(text);

while (text[length] != '\0')
length++;

printf("%d",length);
}

我试图使用上面的程序来计算字符串中的字母。有效。但是,当我尝试使用 scanf() 相同的程序时而不是gets() ,没有成功。

最佳答案

gets(buf)scanf("%[^\n]", buf) 是等效的,并且不得使用,因为他们将读取超出 buf 末尾的输入。

使用fgets(buf, sizeof buf, stdin)并调整您的代码,因为fgets'\n'存储在buf 的末尾(如果存在于输入流中)。

关于c - gets() 和 scanf() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28774091/

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