这个循环将执行多少次?

时间:2021-02-08 23:55:48

I write this code, which takes an integer number (t) as input from the user. A loop will be executed just 't' times. But I find that it runs for (t-1) times. For example, if I give input 3, it runs only 2 times. Can anyone please explain why this is happening?

我编写这个代码,它接受一个整数(t)作为用户的输入。循环将在t' times执行。但我发现它运行的是(t-1)次。例如,如果我输入3,它只运行2次。谁能解释一下为什么会这样吗?

I tried and used scanf("%s", &str), it works, but then I can't take a string as input that contains spaces.

我尝试并使用scanf(“%s”和&str),它可以工作,但是我不能使用包含空格的字符串作为输入。

#include <stdio.h>
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
      {
        char str[100];
        gets(str);
        printf("%s\n", str);
      }
    return 0;
}

3 个解决方案

#1


2  

scanf("%d", &t) consumes only the numeral in the input stream and leaves the remaining characters. When you enter a numeral and press enter, there is a newline character after the numeral.

scanf(“%d”,&t)只使用输入流中的数字并保留其余的字符。当您输入一个数字并按enter时,在数字之后会有一个换行字符。

The first gets reads this newline and returns a string that is empty except for the newline. The first iteration of the loop prints this blank line.

第一个读取这个换行,并返回一个空的字符串,除了换行符。循环的第一次迭代打印此空行。

#2


1  

Loop is iterating 3 times as it should.But it seems that it is iterating 2 times only because of the reason that the \n character left behind by gets in the buffer is read in second iteration.
For first iteration, when you enter a string and the press Enter, the \n character go to the buffer with the string. gets stop reading when it encounters \0, leaving \n in the buffer. On next iteration this \n (non printable character) is read by gets and then printed to terminal.

循环重复了三次。但它似乎只迭代了2次,原因是get在缓冲区中留下的\n字符在第二次迭代中被读取。对于第一次迭代,当您输入一个字符串并按下enter时,\n字符将使用字符串进入缓冲区。当它遇到\0时停止读取,将\n留在缓冲区中。在下一次迭代中,这个\n(不可打印字符)被get读取,然后打印到终端。


NOTE: Never use gets function. It is no longer is the part of standard C. Use fgets instead.

注意:永远不要使用get函数。它不再是标准c的一部分,而是使用fgets。

#3


0  

I guess you can also use the scanf function to resolve your problem that the string is not accepting anything after a SPACE . Also, the loop is running (t-1) times because the buffer is not being cleared due to the use of gets() . In order to resolve this, you can use the getch() function to clear the buffer. The following code should work I guess,

我猜您还可以使用scanf函数来解决字符串在空格后不接受任何内容的问题。此外,循环正在运行(t-1)次,因为由于使用了gets(),缓冲区没有被清除。为了解决这个问题,您可以使用getch()函数来清除缓冲区。下面的代码应该可以工作,

#include <stdio.h>
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
     {
       char str[100];
       scanf("%[^\n]c",&str);
       printf("%s\n", str);
       getch();
     }
return 0;

}

}

In this code, the getch() will clear the buffer by accepting the useless value. Also as far as scanf() is considered, the ^ inside the scanf tells the function that it needs to take the input till the character after the ^ is encountered which in this case is an escape sequence of NEW LINE . I have tried using some small words as well instead of the newline and it has worked as well. Hope this clears your issue :)

在这段代码中,getch()将通过接受无用的值来清除缓冲区。也至于scanf()被认为是,里面的^ scanf告诉函数,它需要输入到字符^后遇到了在这种情况下是一个转义序列的新行。我也试过用一些小单词,而不是换行,它也起作用了。希望这能解决你的问题:)

#1


2  

scanf("%d", &t) consumes only the numeral in the input stream and leaves the remaining characters. When you enter a numeral and press enter, there is a newline character after the numeral.

scanf(“%d”,&t)只使用输入流中的数字并保留其余的字符。当您输入一个数字并按enter时,在数字之后会有一个换行字符。

The first gets reads this newline and returns a string that is empty except for the newline. The first iteration of the loop prints this blank line.

第一个读取这个换行,并返回一个空的字符串,除了换行符。循环的第一次迭代打印此空行。

#2


1  

Loop is iterating 3 times as it should.But it seems that it is iterating 2 times only because of the reason that the \n character left behind by gets in the buffer is read in second iteration.
For first iteration, when you enter a string and the press Enter, the \n character go to the buffer with the string. gets stop reading when it encounters \0, leaving \n in the buffer. On next iteration this \n (non printable character) is read by gets and then printed to terminal.

循环重复了三次。但它似乎只迭代了2次,原因是get在缓冲区中留下的\n字符在第二次迭代中被读取。对于第一次迭代,当您输入一个字符串并按下enter时,\n字符将使用字符串进入缓冲区。当它遇到\0时停止读取,将\n留在缓冲区中。在下一次迭代中,这个\n(不可打印字符)被get读取,然后打印到终端。


NOTE: Never use gets function. It is no longer is the part of standard C. Use fgets instead.

注意:永远不要使用get函数。它不再是标准c的一部分,而是使用fgets。

#3


0  

I guess you can also use the scanf function to resolve your problem that the string is not accepting anything after a SPACE . Also, the loop is running (t-1) times because the buffer is not being cleared due to the use of gets() . In order to resolve this, you can use the getch() function to clear the buffer. The following code should work I guess,

我猜您还可以使用scanf函数来解决字符串在空格后不接受任何内容的问题。此外,循环正在运行(t-1)次,因为由于使用了gets(),缓冲区没有被清除。为了解决这个问题,您可以使用getch()函数来清除缓冲区。下面的代码应该可以工作,

#include <stdio.h>
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
     {
       char str[100];
       scanf("%[^\n]c",&str);
       printf("%s\n", str);
       getch();
     }
return 0;

}

}

In this code, the getch() will clear the buffer by accepting the useless value. Also as far as scanf() is considered, the ^ inside the scanf tells the function that it needs to take the input till the character after the ^ is encountered which in this case is an escape sequence of NEW LINE . I have tried using some small words as well instead of the newline and it has worked as well. Hope this clears your issue :)

在这段代码中,getch()将通过接受无用的值来清除缓冲区。也至于scanf()被认为是,里面的^ scanf告诉函数,它需要输入到字符^后遇到了在这种情况下是一个转义序列的新行。我也试过用一些小单词,而不是换行,它也起作用了。希望这能解决你的问题:)