0
    #include <stdio.h>
    int main(void)
    {
        int array[10] = {};
        scanf("%d\n", array);

        for (int i = 0; i < 10; i++)
        {
            printf("%i ", array[i]);
        }
    }

Why am i asked for input twice by scanf??

2 Answers 2

2

Per the manual page of scanf()

A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

when something like

scanf("%d\n", array);

is executed, scanf() expects to read an int followed by any amount of white-spaces (e.g., space, tab, newline, etc) including none at all.

Since you specified a newline character in the format and since \n means any amount of white-spaces (including none), scanf() continues reading after the first newline looking for more white-spaces that could be there to read. So scanf() will keep prompting you until a non-white-space character is inputted. At this moment it will stop reading!

0

Umm, i just noticed that there is a \n inside scanf. I think that is causing the error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .