using scanf("%d ") with a space after the %d

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);

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));
    }
}

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


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);

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.

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.

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

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

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.