0

Why does the following scanf statement accept 4 inputs, while it has to accept 3 inputs?

#include<stdio.h>    
int main()
{
  int a,b,c;
  scanf("%d\n%d\n%d\n",&a,&b,&c);
  return 0;
}

It has to accept 3 inputs but expects and accepts 4 inputs. It doesn't work if I give 3 inputs. If I remove the 3rd \n character, then the program accepts 3 inputs.

4 Answers4

2

The program only appears to accept 4 inputs into scanf().

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

With each "%d, scanf() reads
1) any leading white-space including ' ' and '\n' and other white-space then
2) it reads in the digits to form the int until it reads a non-digit, usually the \n.
3) This extra char is put back into stdio for subsequent scanning.

The \n tells scanf() to read in, and not save, any number of white-space, not just only '\n'. Reads will continue until a non-white-space is read. That non-white-space is put back into stdio for subsequent scanning.

So with 3 sets of "%d\n", once needs to enter the below for scanf() to read 3 ints and complete.

optional-white-space  (+-)[1 or more 0-9]  optional-white-space
optional-white-space  (+-)[1 or more 0-9]  optional-white-space
optional-white-space  (+-)[1 or more 0-9]  optional-white-space 1-non-white-space

Remember the non-white-space was put back for subsequent I/O. This short program never tried reading it again.

Since stdin is typically buffered, that last non-white-space needs a following \n before the console gives the line to stdin.


The best way to handle user I/O is to first get the line of text with fgets(), then use sscanf(), strtol(), strtof() or strtok() to parse the received line.

// Example reading 1 `int` per line
int a[3];
for (int i=0; i<3; i++) {
  char buf[40];
  if (fgets(buf, sizeof buf, stdin)== NULL) Handle_EOForIOError();
  if (sscanf(buf, "%d", &a[i]) != 1) Handle_ParseError();
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

scanf functions the same thing which is written into "".So if you write scanf("\n%d\n%d\n",&a,&b); it is accepting a newline digit newline digit newline. so for the sake of simplicity we write scanf("%d%d",&a,&b); if we have to take two inputs.

Taranjeet
  • 583
  • 2
  • 9
  • 20
0

Try removing \n from your scanf

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

More info here.

Community
  • 1
  • 1
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • yeah it works then..but why does it accept 4 inputs if I give \n – user3219361 Jan 25 '14 at 04:46
  • @user3219361 Go through the link posted. And if you consider `\n` as an input (which is actually not considered as one and is not stored in your variables) then you are infact giving 6 _inputs_ and not 3 as you would expect. – Suvarna Pattayil Jan 25 '14 at 04:49
  • As you have mentioned \n in your scanf, it simply expect any non-white-space input. – Digital_Reality Jan 25 '14 at 04:54
-2

When we give "\n" in "scanf",it means that the number our input given till we press enter is ought to be considered as one string but here we don't have enough variables to store the inputs seperated by "space" as well as "enter" so the extra variables are discarded and the once that are stored in the variables for the first time are used as the primary values but if we dynamically allocate the memory in an array and then put something like "array[^\n]" in the scanf then in can consider the whole entered input as a single string.