2

When using the scanf function, I've been using a format string like scanf("%d",&x);. However, I'm curious about something for few days ago, so I'm asking a question.

What happens when you put a character in the quotation marks of the scanf function?

Example:

printf("Please enter the date you were born: ex.1999/5/6 : ")
scanf("%d/%d/%d", &year, &month, &date);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

4

scanf reads a data stream from stdin and tries to parse your format string. If it doesn't fit, it stops processing. It is important to understand that the rest of the input remains in stdin stream for subsequent reads. scanf returns the number of values that have successfully been parsed.

From my experience it is best to write a small program and test the behavior by yourself:

#include "stdio.h"

void main() {
        int year = 0;
        int month = 0;
        int date = 0;
        int count;

        while (1) {
                printf("enter data:\n");
                count = scanf("%d/%d/%d", &year, &month, &date);
                printf("data received:\n");
                printf("%d-%d-%d count=%d\n", year, month, date, count);
        }
}

Example 1: Fresh start and enter correct data:

enter data:
1/2/3
data received:
1-2-3 count=3

Example 2: Fresh start and enter wrong data:

enter data:
1 2 3
data received:
1-0-0 count=1
enter data:
data received:
2-0-0 count=1
enter data:
data received:
3-0-0 count=1

You can see that scanf reads the data only until the space after 1 because it doesn't fit the format string. scanf only changes the value of the first variable. Still the rest of your input, which is 2 3, is in stdin. The loop continues and the next call to scanf reads the 2. The space in front of the number is skipped. This happens without any user interaction! Again no slash is found. And so on. Try it yourself with different cases and understand that using scanf is a dangerous thing ... and if you don't initialise the variables and parsing fails you will get uninitialised data!

Markus
  • 5,976
  • 5
  • 6
  • 21
  • 1
    +1 for noting the important fact that input remains in the stream at the point of failure. Also, any character that tests positive for `isspace` consumes any number of `isspace` characters. – Neil Apr 08 '22 at 19:50
3

What happens when you put a character in the quotation marks of the scanf function?

It causes the function to skip one occurrence of that exact character from the input stream, providing that the next character to be read is a match. If the next character is not a match, then the call to scanf will fail at that point.

From cppreference:

The format string consists of

  • non-whitespace multibyte characters except %: each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal.

So, consider the following:

int i, j;
int count = scanf("%dbob%d", &i, &j);

For an input of 1bob2, this will successfully read the values of 1 and 2 into i and j, respectively (skipping the 3 bob characters), and the returned count value will be 2.

However, for an input of 1Bob2, the 1 will be read into i and then the call will fail, because the next character (B) is not b, and the returned count value will be only 1. The same will happen for 1bOb2 and 1boB2.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83