简体   繁体   English

具有多个单词的字符串的 scanf() 行为

[英]scanf() behaviour for strings with more than one word

Well I've been programming in C for quite a while now, and there is this question about the function scanf()好吧,我已经用C编程了很长一段时间了,关于函数scanf()有这个问题

here is my problem:这是我的问题:

I know that every element in ASCII table is a character and I even know that %s is a data specified for a string which is a collection of characters我知道 ASCII 表中的每个元素都是一个字符,我什至知道 %s 是为字符串指定的数据,该字符串是字符的集合

My questions:我的问题:

1.why does scanf() stops scanning after we press enter. 1.为什么我们按回车后scanf()停止扫描。 If enter is also character why cant it be added as a component of the string that is being scanned.如果输入也是字符,为什么不能将其添加为正在扫描的字符串的组成部分。

2.My second question and what I require the most is why does it stops scanning after a space, when space is again a character? 2.我的第二个问题也是我最需要的是为什么它在空格后停止扫描,而空格又是一个字符?

Note: My question is not about how to avoid these but how does this happen注意:我的问题不是关于如何避免这些,而是​​关于这是如何发生的

I'd be happy if this is already addressed, I'd gladly delete my question and even if I've presumed something wrong please let me know如果这已经得到解决,我会很高兴,我很乐意删除我的问题,即使我认为有问题也请告诉我

"why does scanf() stops scanning after we press enter." “为什么我们按回车后 scanf() 停止扫描。” is not always true.并非总是如此。

The "%s" directs scanf() as follows "%s"指令scanf()如下

char buffer[100];
scanf("%s", buffer);
  1. Scan and consume all white-space including '\\n' generated from multiple Enter s.扫描并使用所有空格,包括从多个Enter生成的'\\n' This data is not saved.不保存此数据。

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [ , c , or n specifier C11dr §7.21.6.2 8输入空白字符(由isspace函数指定)将被跳过,除非规范包含[cn说明符 C11dr §7.21.6.2 8

  1. Scan and save all non-white-space characters.扫描并保存所有非空白字符。 Continue doing so until a white-space is encountered.继续这样做,直到遇到空格。

Matches a sequence of non-white-space characters §7.21.6.2 12匹配一系列非空白字符 §7.21.6.2 12

  1. This white-space is put back into stdin for the next input function.这个空白被放回到stdin以供下一个输入函数使用。 (OP's 2nd question) (OP的第二个问题)
  2. A null character is appended to buffer .一个空字符被附加到buffer
  3. Operations may stop short if EOF occurs.如果发生 EOF,操作可能会短暂停止。
  4. If too much data is save in buffer , it is UB.如果buffer保存的数据过多,则为UB。
  5. If some non-white-space data is saved, return 1. If EOF encountered, return EOF.如果保存了一些非空白数据,则返回1。如果遇到EOF,则返回EOF。

Note: stdin is usually line buffered, so no keyboard data is given to stdin until a '\\n' occurs.注意: stdin通常是行缓冲的,因此在出现'\\n'之前不会将键盘数据提供给stdin

From my reading of your question, both of your numbered questions are the same:根据我对您问题的阅读,您的两个编号问题是相同的:

Why does scanf with a format specifier of %s stop reading after encountering a space or newline.为什么带有%s格式说明符的scanf在遇到空格或换行符后停止读取。

And the answer to both of your questions is: Because that is what scanf with the %s format specifier is documented to do.您的两个问题的答案是:因为这是记录了带有%s格式说明符的scanf要做的事情。

From the documentation :文档

%s Matches a sequence of bytes that are not white-space characters. %s匹配非空白字符的字节序列。

A space and a newline character (generated by the enter key) are white-space characters.空格和换行符(由回车键生成)是空白字符。

I made miniprogram with scanf for get multiple name without stop on space or ever enter.我用 scanf 制作了小程序,以获得多个名称,而不会在空间上停留或进入。 i use while我用的时候

Scanf("%s",text);

While (1)
{
Scanf("%s",text1)
If (text1=='.'){break;}
//here i simple add text1 to text
}

This way i get one line if use the .这样,如果使用 . Now i use scanf("%[^\\n]",text);现在我使用 scanf("%[^\\n]",text); It work great.它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM