当格式字符串末尾的sa newline时,为什么SCANF两次要求输入?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
当我运行上面的代码时,提示询问我两次输入(我仅在代码中使用scanf
)。这是为什么?
(我输入了'jo';它要求提供更多输入,所以我再次输入了'jo'。然后它才打印出“ jo”一次。)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
When I run the code above, the prompt is asking me twice for input (I only use scanf
once in the code). Why is that?
(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从我的SCANF手册页面
因此,使用
scanf(“%s \ n”,a)
它将扫描字符串,然后是可选的白色空间。由于在第一个新线之后,可能会随后更加清晰,因此SCANF并未在第一个Newline之后完成,并且下一步是什么。您会注意到,您可以输入任意数量的新线(或标签或空格),而SCANF仍将等待更多。但是,当您输入第二个字符串时,将对空格的序列进行界定并扫描停止。
使用
scanf(“%s”,a)
不要扫描尾随空格。From my scanf manual page
Thus with
scanf ("%s\n", a)
it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.
Use
scanf ("%s", a)
to not scan trailing whitespace.您必须从
scanf
的字符串格式中删除\ n
。它应该是编辑:说明
%s
意味着SCANF读取输入字符,直到获得一个定界符,该定界符应该是空间或tab或tab等新空间或新行(\ n
)因此,第一个输入是作为“%s”
的定界符,并将“ \ n”
添加到字符串格式中“%s \ n”
表示SCANF将等待2个新线,第一个newline与“%s”
的定界符有关,第二个newline与字符串格式的\ n
。you have to remove the
\n
from the string format of thescanf
. It should beEDIT: Explanation
the
%s
means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n
) so the first enter is get as a delimiter for the"%s"
and adding the"\n"
to the string format"%s\n"
means that the scanf will wait 2 newlines the first newline is related to the delimiter of the"%s"
and the second newline is related to the\n
of the string format.从SCANF格式中删除
\ n
,并给出输入,并根据给定的输出一次显示输出。Remove
\n
from the scanf format and give an input and it displays the output based on the given output once.您可以使用其中的任何一个来避免上述问题:
scanf(“%s”,a);
或者
scanf(“ \ n%s”,a);
you can use either of these to avoid the mentioned problem :
scanf("%s",a);
or
scanf("\n%s",a);
尝试以下尝试:不要在scanf上使用\ n,它不会问您两次,有时可能会显示错误
您的代码:
scanf(“%s \ n”,a) ;
在scanf 上尝试一下:
scanf(“%s”,a);
Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error
Your code:
scanf("%s\n", a);
Try this on scanf:
scanf("%s", a);
使用get get()或fgets()而不是...或者使用scanf(“%[^\ n] s”,a);
use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);
不要在scanf stdio函数中使用逃生序列
Don't use the escape sequence in scanf stdio function