简体   繁体   English

使用scanf for for循环

[英]Using scanf in for loop

Here is my c code: 这是我的c代码:

int main()
{
    int a;
    for (int i = 0; i < 3; i++)
        scanf("%d ", &a);

    return 0;
}

When I input things like 1 2 3 , it will ask me to input more, and I need to input something not ' '. 当我输入像1 2 3这样的东西时,它会要求我输入更多,我需要输入不是''的东西。

However, when I change it to (or other thing not ' ' ) 但是,当我将其更改为(或其他不是' ' )时

scanf("%d !", &a);

and input 1 ! 2! 3! 并输入1 ! 2! 3! 1 ! 2! 3! , it will not ask more input. ,它不会要求更多的输入。

The final space in scanf("%d ", &a); scanf("%d ", &a);的最后一个空格scanf("%d ", &a); instructs scanf to consume all white space following the number. 指示scanf消耗该数字后的所有空格。 It will keep reading from stdin until you type something that is not white space. 它会继续读取stdin直到你键入不是空格的东西。 Simplify the format this way: 以这种方式简化格式:

scanf("%d", &a);

scanf will still ignore white space before the numbers. scanf仍会在数字前忽略空格。

Conversely, the format "%d !" 相反,格式为"%d !" consumes any white space following the number and a single ! 消耗数字和单个后面的任何空格! . It stops scanning when it gets this character, or another non space character which it leaves in the input stream. 它在获取此字符时停止扫描,或者在输入流中留下另一个非空格字符。 You cannot tell from the return value whether it matched the ! 你无法从返回值告诉它是否与之匹配! or not. 或不。

scanf is very clunky, it is very difficult to use it correctly. scanf 非常笨重,很难正确使用它。 It is often better to read a line of input with fgets() and parse that with sscanf() or even simpler functions such as strtol() , strspn() or strcspn() . 通常使用fgets()读取一行输入并使用sscanf()甚至更简单的函数(如strtol()strspn()strcspn() fgets()进行解析通常会更好。

scanf("%d", &a); 

这应该做的工作。

Basically, scanf() consumes stdin input as much as its pattern matches. 基本上, scanf()消耗stdin输入和模式匹配。 If you pass "%d" as the pattern, it will stop reading input after a integer is found. 如果传递"%d"作为模式,它将在找到整数后停止读取输入。 However, if you feed it with "%dx" for example, it matches with all integers followed by a character 'x' . 但是,如果您使用"%dx"来提供它,它会匹配所有整数,后跟一个字符'x'

More Details: 更多细节:

Your pattern string could have the following characters: 您的模式字符串可以包含以下字符:

  • Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). 空白字符:该函数将读取并忽略在下一个非空白字符之前遇到的任何空白字符(空白字符包括空格,换行符和制表符 - 请参阅isspace)。 A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none). 格式字符串中的单个空格验证从流中提取的任何数量的空白字符(包括无)。

  • Non-whitespace character, except format specifier (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. 非空白字符,格式说明符(%)除外:任何不是空格字符(空格,换行符或制表符)或格式说明符的一部分(以%字符开头)的字符都会导致该函数读取下一个字符从流中,将其与此非空白字符进行比较,如果匹配,则将其丢弃,并且函数将继续使用格式的下一个字符。 If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread. 如果字符不匹配,则函数失败,返回并保留流的后续字符未读。

  • Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from the stream and stored into the locations pointed by the additional arguments. 格式说明符:由初始百分号(%)组成的序列表示格式说明符,用于指定要从流中检索并存储到其他参数指向的位置的数据的类型和格式。

Source: http://www.cplusplus.com/reference/cstdio/scanf/ 资料来源: http//www.cplusplus.com/reference/cstdio/scanf/

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

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