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

查看:19
本文介绍了使用 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 
", 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() 匹配零个或多个空白字符,直到匹配失败.空格 (' ')、换行符 (' ')、回车符 (' ') 和制表符 (' ') 属于空白字符.当格式字符串末尾出现空格时,scanf() 将尝试匹配输入中的空白字符,直到找不到匹配项为止.但是,scanf() 只能在匹配失败或到达文件末尾时返回.因此,在如下语句的情况下:

A space in the format string tells scanf() to match zero or more whitespace characters, until the match fails. Spaces (' '), newlines(' '), carriage returns (' '), and tabs (' ') 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() 格式字符串几乎总是一个错误.换行符 (' ') 也是一个空白字符,放置在格式字符串的末尾时具有相同的效果.

It is almost always a mistake to terminate a scanf() format string with a space. A newline (' ') 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() 在要转换的输入字符之间期望零个或多个空白字符.这允许用户输入具有中间空格的字符.如果没有添加空格,如果用户输入 ab 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 , 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天全站免登陆