简体   繁体   English

C - scanf 输入如何工作以及它如何在循环中迭代?

[英]C - How scanf inputs work and how it iterates in loops?

I know the basics of scanf();我知道 scanf() 的基础知识; requiring a conversion specifier % and an address to the variable to store the input value &.需要转换说明符 % 和变量地址来存储输入值 &。

While doing an assignment, there was a task that involved inputting an arbitrary number of values, one example would be: 5 4 3 2 1在进行分配时,有一项任务涉及输入任意数量的值,例如:5 4 3 2 1

Into scanf, and then print out number of symbols corresponding to the integer typed (eg. '?'), so it would display the terminal as:进入scanf,然后打印出integer类型对应的符号数(例如'?'),所以终端显示为:

5 4 3 2 1
?????
????
???
??
?

In this case I had put the scanf in a while loop as so:在这种情况下,我将 scanf 置于 while 循环中,如下所示:

int i, num;
while(scanf("%d",&num)==1){
   printf("%d",num);
   for (i=0; i<num; i++){
      printf("?");
   }
   printf("/n");
}

I am totally confused how scanf reads multiple integers in one input line (5 4 3 2 1) when many sources has specified that scanf only takes one integer until it reads a whitespace.当许多来源指定 scanf 只需要一个 integer 直到它读取一个空格时,我完全混淆了 scanf 如何在一个输入行(5 4 3 2 1)中读取多个整数。 From looking at this, my understanding is that the whitespace separating the integers indicate a new iteration following the previous integer?从这个角度来看,我的理解是分隔整数的空格表示在之前的 integer 之后的新迭代? When I tried to trace how printing works, it printed as:当我试图追踪打印的工作原理时,它打印为:

5 4 3 2 1
5?????
4????
3???
2??
1?

...So my question is how does scanf 'save' all these integers in one line to 'num' and print EACH of the corresponding symbols to the values given AFTER input? ...所以我的问题是 scanf 如何将所有这些整数在一行中“保存”到“num”并将每个相应的符号打印到输入后给定的值? Wouldn't integers be replacing the previous in the variable without an array?如果没有数组,整数不会替换变量中的前一个吗?

Sorry if this question does not make sense - still quite new to coding.抱歉,如果这个问题没有意义 - 对编码来说还是很新的。 Thank you!谢谢!

Your understanding of scanf is true, scanf only takes one integer until it reads a whitespace.你对scanf的理解是对的,scanf 只需要一个 integer 直到它读取一个空格。

What makes you confused here is how the algorithm works.在这里让你感到困惑的是算法是如何工作的。 Your algorithm first reads the integer 5 and prints "immediately" five ?您的算法首先读取 integer 5 并“立即”打印 5 ? . . It continues to read 4, print four ?它继续读 4,打印 4 ? , read 3, print three ? , 读 3, 打印 3 ? , and so on. , 等等。

scanf stores the line in an internal buffer. scanf将行存储在内部缓冲区中。 If the buffer is not empty, it tries to read from the buffer and remove the portion that was successfully read;如果缓冲区不为空,则尝试从缓冲区中读取并删除成功读取的部分; if the buffer is empty it waits for user input.如果缓冲区为空,则等待用户输入。 On the first iteration of your outer loop ( while ), the buffer is empty.在外循环( while )的第一次迭代中,缓冲区是空的。 You input "5 4 3 2 1", so the buffer contains that.您输入“5 4 3 2 1”,因此缓冲区包含它。 The first sscanf("%d",&num) reads the first integer from the buffer, which is 5;第一个sscanf("%d",&num)从缓冲区中读取第一个 integer,即 5; the buffer now contains " 4 3 2 1".缓冲区现在包含“4 3 2 1”。 On the second iteration you read 4, and the buffer will be " 3 2 1".在第二次迭代中,您读取 4,缓冲区将为“3 2 1”。 This goes on until the buffer is exhausted.这种情况一直持续到缓冲区用完为止。 At this point your program continues;此时您的程序继续; it just waits for more input.它只是等待更多的输入。

Wouldn't integers be replacing the previous in the variable without an array?如果没有数组,整数不会替换变量中的前一个吗?

There's no array required here at all.这里根本不需要数组。

The scanf() function takes the value (assuming white spaces included) and all of them are saved one by one into the variable num . scanf() function 取值(假设包括空格)并将它们一一保存到变量num中。 If you use a debugger (suppose using GDB) and set a breakpoint in while() , you'll get to know by stepping into that the variable num is changed in each iteration and the for loop runs till num is reached.如果您使用调试器(假设使用 GDB)并在while()中设置断点,您将通过逐步了解变量num在每次迭代中更改并且for循环运行直到达到num来了解。

An example of two iterations using a debugger is provided below (focus on num on the left corner):下面提供了使用调试器进行两次迭代的示例(关注左角的num ):

Iteration 1:迭代1:

迭代 1

Iteration 2:迭代 2:

迭代 2

scanf 'reads' characters from the standard input stream stdin (if there is nothing there yet it prompts the user to type something to be added to it), and converts them to a format depending on the conversion specifiers used in the format string. scanf从标准输入stdin标准输入中“读取”字符(如果还没有任何内容,它会提示用户输入要添加的内容),并将它们转换为取决于格式字符串中使用的转换说明符的格式。

You can think about stdin as a sequence of characters in the memory, and every time the user types something in the terminal and presses Enter what was typed is added to the stdin .您可以将stdin视为 memory 中的字符序列,每次用户在终端中键入内容并按 Enter 时,键入的内容都会添加到stdin

So in your case, when you type 5 4 3 2 1 and presses Enter in the terminal, stdin will receive the sequence of characters "5 4 3 2 1\n" (notice that the new line character was added when you pressed Enter).因此,在您的情况下,当您在终端中键入5 4 3 2 1并按 Enter 时, stdin将收到字符序列"5 4 3 2 1\n" (请注意,当您按 Enter 时添加了换行符) .

Then the scanf("%d",&num) , which is expecting only 1 decimal integer number, 'reads' and 'consumes' from stdin the relevant characters that form one continuous decimal integer number.然后scanf("%d",&num) ,它只需要 1 个十进制 integer 数字,从标准输入中“读取”和“消耗”形成一个连续十进制stdin数字的相关字符。

In this case 5 is read the first time scanf is executed, and " 4 3 2 1\n" is left in the stdin .在这种情况下,第一次执行scanf时会读取5 ,并将" 4 3 2 1\n"留在stdin中。 Then as the condition is satisfied the loop statements are executed.然后在满足条件时执行循环语句。 And the second time scanf is executed the leading space is discarded by the %d specifier and 4 is read, leaving " 3 2 1\n" in the stdin .第二次执行scanf时,前导空格被%d说明符丢弃并读取4 ,在stdin中留下" 3 2 1\n" And so on until no decimal integers numbers can be read from the stdin ...依此类推,直到无法从stdin中读取十进制整数...

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

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