Search code examples
cscanfstdio

Why space doesn't as same as enter when I use scanf?


I have this code:

int a, b;
scanf("%d", &a);
scanf("%d", &b);

printf("%d", a + b);

when I have this input the program works fine:

1<enter>2<enter>

and returns 3 (as expected!)

but when I try to enter my inputs using <space> key:

1<space>2<space>

nothing happens and I have to press <enter> after all to make my program to go to next line.

So what's the problem? why space works sometime and wont works some other times?


Solution

  • stdin is often line-buffered, not immediate.

    Thus, prior to pressing enter, your program does not get the input at all.