Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
printf和scanf如何在循环中工作?为什么我不需要在scanf中\n?_C_While Loop_Printf_Scanf_Do While - Fatal编程技术网

printf和scanf如何在循环中工作?为什么我不需要在scanf中\n?

printf和scanf如何在循环中工作?为什么我不需要在scanf中\n?,c,while-loop,printf,scanf,do-while,C,While Loop,Printf,Scanf,Do While,我真的不理解下面的代码。它到底是如何工作的(我指的是I/O缓冲区)。我的代码中不需要\n字符,它仍然有效!谁能一步一步地给我解释一下吗 #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int x = -1; do { printf("Give x: "); scanf("%d", &x); }whil

我真的不理解下面的代码。它到底是如何工作的(我指的是I/O缓冲区)。我的代码中不需要
\n
字符,它仍然有效!谁能一步一步地给我解释一下吗

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int x = -1;

    do
    {
        printf("Give x: ");
        scanf("%d", &x);
    }while(x<=0);

    printf("x = %d\n", x);

    x = -1;

    while(x<=0)
    {
        printf("Give x: ");
        scanf("%d", &x);
    }

    printf("x = %d\n", x);

    return 0;
}
#包括
#包括
int main(int argc,字符**argv)
{
int x=-1;
做
{
printf(“给出x:”);
scanf(“%d”和&x);
}而(x根据:

这就是为什么您不需要在
scanf
中指定
\n
,下一次
scanf
调用将忽略它。

根据:


这就是为什么您不需要在
scanf
中指定
\n
,下一次
scanf
调用将忽略它。

去掉您可能理解的部分,查看
scanf()
调用

%d”
格式说明符要求扫描可选的空白(空格、制表符、\n等),然后扫描
整型
。这通常持续到遇到不属于
整型
的字符。然后该字符被“取消锁定”(放回输入缓冲区)

假设您的输入是
“123-456”
。在第一个while循环之后,
“-456”
将保留在输入缓冲区中。第二个while循环将消耗
“-456”
。假设
stdin
-456
之后关闭,scanf()然后将检测到没有更多数据,并将
x
设置为值-456。由于
x
仍然为负值,第二个while循环再次执行
scanf()
。这一次,没有数据和
scanf()
不会更改
x
,并返回EOF,遗憾的是没有监控。结果:无止境循环

do {
  ...
  scanf("%d", &x);
} while (x<=0);
...
x = -1;
while (x<=0) {
  ...
  scanf("%d", &x);
}
现在尝试
“123a 456”
。在第一个while循环之后
“a 456”
将保留在输入缓冲区中。第二个while循环将调用
scanf()
,并且无法转换任何内容,因为
a
没有开始一个数字-因此
x
保留为-1。
scanf
将返回0,不幸的是没有进行监视。未开始消耗的
a
将保留在输入缓冲区中。第二个while循环再次调用
scanf()
,这将执行相同的操作,导致无休止的循环

do {
  ...
  scanf("%d", &x);
} while (x<=0);
...
x = -1;
while (x<=0) {
  ...
  scanf("%d", &x);
}
do{
...
scanf(“%d”和&x);

}在(x剥离您可能理解的部分时,请查看
scanf()
调用

%d”
格式说明符要求扫描可选的空白(空格、制表符、\n等),然后扫描
整型
。这通常持续到遇到不属于
整型
的字符。然后该字符被“取消锁定”(放回输入缓冲区)

假设您的输入是
“123-456”
。在第一个while循环之后,
“-456”
将保留在输入缓冲区中。第二个while循环将消耗
“-456”
。假设
stdin
-456
之后关闭,scanf()然后将检测到没有更多数据,并将
x
设置为值-456。由于
x
仍然为负值,第二个while循环再次执行
scanf()
。这一次,没有数据和
scanf()
不会更改
x
,并返回EOF,遗憾的是没有监控。结果:无止境循环

do {
  ...
  scanf("%d", &x);
} while (x<=0);
...
x = -1;
while (x<=0) {
  ...
  scanf("%d", &x);
}
现在尝试
“123a 456”
。在第一个while循环之后
“a 456”
将保留在输入缓冲区中。第二个while循环将调用
scanf()
,并且无法转换任何内容,因为
a
没有开始一个数字-因此
x
保留为-1。
scanf
将返回0,不幸的是没有进行监视。未开始消耗的
a
将保留在输入缓冲区中。第二个while循环再次调用
scanf()
,这将执行相同的操作,导致无休止的循环

do {
  ...
  scanf("%d", &x);
} while (x<=0);
...
x = -1;
while (x<=0) {
  ...
  scanf("%d", &x);
}
do{
...
scanf(“%d”和&x);

}while(x为什么你认为你需要
\n
scanf
中?@haccks:因为当我在
printf
中有
\n
时,
printf(“\nGive x:”);
我需要添加
put(“)在第二个
之前,当
循环以使它正常工作时不。没有必要把
放入
。为什么你认为你需要
\n
放入
scanf
?@haccks:因为当我在
\n
中有
\n
放入
printf
printf(“\nGive x:”;
我需要添加
放入(“);
在第二个
之前,而
循环以使其正常工作否。不需要放置
放置