当格式字符串末尾的sa newline时,为什么SCANF两次要求输入?

发布于 2025-02-11 04:50:24 字数 400 浏览 2 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一梦等七年七年为一梦 2025-02-18 04:50:24

从我的SCANF手册页面

格式字符串中的空白(例如空白,标签或新线)匹配输入中的任何数量的空白,包括无。其他一切都只匹配自己。

因此,使用scanf(“%s \ n”,a)它将扫描字符串,然后是可选的白色空间。由于在第一个新线之后,可能会随后更加清晰,因此SCANF并未在第一个Newline之后完成,并且下一步是什么。您会注意到,您可以输入任意数量的新线(或标签或空格),而SCANF仍将等待更多。

但是,当您输入第二个字符串时,将对空格的序列进行界定并扫描停止。

使用scanf(“%s”,a)不要扫描尾随空格。

From my scanf manual page

White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.

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.

烟酉 2025-02-18 04:50:24

您必须从scanf的字符串格式中删除\ n。它应该是

scanf("%s",a);

编辑:说明

%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 the scanf. It should be

scanf("%s",a);

EDIT: 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.

淡水深流 2025-02-18 04:50:24

从SCANF格式中删除\ n,并给出输入,并根据给定的输出一次显示输出。

Remove \n from the scanf format and give an input and it displays the output based on the given output once.

極樂鬼 2025-02-18 04:50:24

您可以使用其中的任何一个来避免上述问题:
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);

素年丶 2025-02-18 04:50:24

尝试以下尝试:不要在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);

时光是把杀猪刀 2025-02-18 04:50:24

使用get get()或fgets()而不是...或者使用scanf(“%[^\ n] s”,a);

use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);

命硬 2025-02-18 04:50:24

不要在scanf stdio函数中使用逃生序列

     scanf ("%s", a);

Don't use the escape sequence in scanf stdio function

     scanf ("%s", a);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文