Take the 2-minute tour ×

I've got a question about how the scanf function works. In this program:

#include <stdio.h>

int main(void) {

    int x;
    char y;

    printf("Write a number: ");
    scanf("%d", &x);

    printf("Write a character: ");
    scanf(" %c", &y);

    printf("%d", x);
    printf("%c", y);

    return 0;

If the user writes a number then hit enter then the scanf function would see from the format string that is should expect a number. When the user taps enter it would not read the new-line character and put it back in the buffer. Then since I am reading a character next I need to have a whitespace in the format string because the scanf function would otherwise take the new-line character and put in the y-variable.

However I am just curious about what happened if the user wrote something like


Would it put everything back in the buffer? And everything that gets "put-back" in the buffer would it automatically be read by the next scanf function in my program?

I'm reading: C Programming A Modern Approach 2nd Edition

share|improve this question
Why not compile the program and just feed it that input to find out? Add logging statements or use a debugger if you need to see something going on that isn't already visible. –  Jeremy W. Sherman Apr 22 '11 at 19:40

3 Answers 3

up vote 1 down vote accepted

You should always check the return value from scanf() to ensure that all the conversions you expected were successful.

Given the sample input:


The first scanf() would fail because j cannot be converted to an integer - returning 0 for the number of successful conversions. The second call would succeed, returning 'j' as the character.

If you need the string to be treated as an integer, use fgets() or a relative (perhaps POSIX getline()) to read the string, then strtoul() (or the appropriate relative - strtol(), strtoll(), strtoull(), strtoimax() or strtoumax()), specifying a base of at least 21 to convert the 'j', or 23 to take the 'l' and 'k'.

share|improve this answer

Error handling

scanf is usually used in situations when the program cannot guarantee that the input is in the expected format. Therefore a robust program must check whether the scanf call succeeded and take appropriate action. If the input was not in the correct format, the erroneous data will still be on the input stream and must be read and discarded before new input can be read. An alternative method of reading input, which avoids this, is to use fgets and then examine the string read in. The last step can be done by sscanf, for example.


share|improve this answer

It will put everything that was not matched by scanf back in the buffer. Eg: scanf("%i %i",...) given the input "12 qwe" will leave qwe in the buffer but not 12. (The spec suggests it won't put the space back, and a quick test confirms.) That said, the documentation is rather unclear, and it is probably a bad idea to rely on it without doing plenty of tests.

share|improve this answer
Strictly, it will leave the q as the next character to be read - it won't even see the w let alone the e. The scanf() will return 1 (for one successful conversion). The space will not be put back; it was successfully matched by the space in the format string. –  Jonathan Leffler Apr 22 '11 at 20:00
@Jonathan Leffler, quite true, will edit. –  David X Apr 22 '11 at 20:08
@Jonathan But for clarity, if the format string were "%i%i", the space would be put back, correct? –  Null Set Apr 22 '11 at 20:08
@Null Set: I think that even if the format string contained no space, then the space would still not be put back because scanf() uses at most one character of push back, and the space was a valid start to the second number; the character pushed back is the one that caused the conversion to fail, and that's the 'q' in this example. I suppose I should write the test to prove the point - but I'm feeling too lazy, and it would only show that at least one implementation had (or did not have) that behaviour, not that the standard mandates that behaviour. –  Jonathan Leffler Apr 22 '11 at 20:13
@Null Set, test suggests it won't even then. –  David X Apr 22 '11 at 20:14

Your Answer


