使用scanf(“%d")在%d之后留一个空格 [英] using scanf("%d ") with a space after the %d

查看:617
本文介绍了使用scanf(“%d")在%d之后留一个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在我的c类中,我正在使用scanf()命令感到麻烦,我们只是在学习指针,并且遇到一个问题,要求我们获取一个数组,然后以反向方式打印它,而没有使用[]进行任何声明,而是声明了( int)数组.当然,这似乎是小菜一碟,但是当您不小心写出以下内容时就不会了:

In my c class today I was troubling with the scanf() command, we were just learning pointers and we got a question asking us to get an array, and print it reversed without using the [] for anything but declaring the (int) array. Of course it seems like a piece of cake but not when you accidentally write:

scanf(%d",arr + i);

scanf("%d ", arr + i);

您是否注意到%d之后的空格?当然确实花了我一些时间来解决这个问题,但是由于某种原因使循环变得疯狂,我希望你们能帮助我(和我的老师)弄清楚为什么会发生这种情况.示例:

Did you notice the space after the %d? Sure did take me a while to figure it out but for some reason that makes loops go crazy, and I wanted you guys to help me (and my teachers) to figure out why does that happen. Example:

#include <stdio.h>
#define LEN 10
void arrayInput(int * arr, unsigned int len);
void arrayReverseOutput(int * arr, unsigned int len);

int main(void)
{
    int arr[LEN] = { 0 };
    arrayInput(arr, LEN);
    arrayReverseOutput(arr, LEN);

    system("pause");
    return 0;
}

void arrayInput(int * arr, unsigned int len)
{
    unsigned int i = 0;
    printf("Enter 10 numbers: ");
    for (i = 0; i < len; i++)
    {
        //printf("i = %d \n", i); see what happens when you use this line
        scanf("%d ", arr + i);
    }
}

void arrayReverseOutput(int * arr, unsigned int len)
{
    int i = 0;
    printf("The numbers in reverse order: ");
    for (i = --len; i >= 0; i--)
    {
        printf("%d ", *(arr + i));
    }
}

我真的很想知道该scanf是怎么回事...好像它在第一次运行时需要2个输入,但是仍然设法将输入放置在数组中的正确位置... 感谢您抽出宝贵时间阅读本< 3

I'm really curios to see what's going on with that scanf... it's as if it requires 2 inputs at the first time it runs but somehow still manages to put the inputs in their right position in the array... Thanks for taking your time to read this <3

推荐答案

格式字符串中的空格告诉scanf()匹配零个或多个空格字符,直到匹配失败.空格字符中包括空格(' '),换行符('\n'),回车符('\r')和制表符('\t').当格式字符串的末尾出现空格时,scanf()将尝试匹配输入中的空格字符,直到找不到匹配项为止.但是,scanf()仅在匹配失败或到达文件末尾时才返回.因此,对于类似这样的语句:

A space in the format string tells scanf() to match zero or more whitespace characters, until the match fails. Spaces (' '), newlines('\n'), carriage returns ('\r'), and tabs ('\t') are among the whitespace characters. When a space occurs at the end of a format string, scanf() will try to match whitespace characters from the input until no match is found. But, scanf() can only return when a match fails, or end of file is reached. Thus, in the case of a statement like:

scanf("%d ", arr + i);

scanf()的呼叫似乎挂起,贪婪地等待用户的更多输入.每当按下 Enter 键时,就会发送换行并由scanf()进行匹配,该换行仍在等待匹配失败.或文件结尾.您可以通过在Linux上使用 Ctrl-D 在Windows上使用 Ctrl-C 从键盘上发出信号来表示文件结束,从而避免这种循环.

the call to scanf() will appear to hang, greedily waiting for more input from the user. Whenever the Enter key is pressed, a newline is sent and matched by scanf(), which is still waiting for a failing match. Or end of file. You can escape such a loop by signalling end of file from the keyboard with Ctrl-D on Linux, or Ctrl-C on Windows.

用空格终止scanf()格式的字符串几乎总是一个错误.换行符('\n')也是空格字符,放在格式字符串的末尾具有相同的作用.

It is almost always a mistake to terminate a scanf() format string with a space. A newline ('\n') is also a whitespace character, and has the same effect when placed at the end of a format string.

请注意,空格可以有效地使用scanf()格式的字符串.例如:

Note that spaces can be used effectively in scanf() format strings. For example:

int retval = scanf(" %c %c", &c1, &c2);

在这里,如果先前的IO操作在输入流中留下了换行符(这种情况很少见),则前导空格指示scanf()读取并忽略它.格式字符串中的第二个空格告诉scanf()在要转换的输入字符之间期望零个或多个空格字符.这允许用户输入带有中间空格的字符.如果没有添加空格,如果用户输入a b\n,则c2最终将保留空格字符的值,并且b将留在输入流中,以进行下一个IO操作.另请注意,scanf()返回成功的转换次数,从而使程序可以检查输入是否符合预期.如果上一行中的retval不是2以外的其他内容,则说明出现了问题.

Here, if a previous IO operation has left a newline in the input stream (a not uncommon occurrence), the leading whitespace directs scanf() to read and ignore it. The second space in the format string tells scanf() to expect zero or more whitespace characters between the input characters to be converted. This allows the user to input the characters with an intervening space. Without the added whitespace, if a user entered a b\n, c2 would end up holding the value for a space character, and the b would be left behind in the input stream for the next IO operation to pick up. Also note that scanf() returns the number of successful conversions, allowing the program to check whether the input is as expected. If retval in the above line is anything other than 2, something has gone wrong.

这篇关于使用scanf(“%d")在%d之后留一个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆