gpt4 book ai didi

c - C中不同类型的scanf用法

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

我知道为了使用 scanf 获取包含空格的输入字符串,我们使用如下调用:

scanf("%[^\n]",str); 

它将匹配所有字符直到新行。但是我发现了另一个scanf的调用:

scanf("%[ A-Za-z]",str);

对我来说两者都工作正常!但我无法弄清楚两者之间有什么区别!?

在处理带空格的字符串时应该使用哪种方法?

最佳答案

what is the difference between the two!?

第一个扫描所有内容直到换行符,第二个只扫描字母和空格。

Which method is should i use while dealing with string with spaces?

都不是,看看fgets()相反:

char buf[LINE_MAX];
fgets(buf, sizeof(buf), stdin);

关于c - C中不同类型的scanf用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17000854/

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