Home > Software engineering >  Using messages inside format control string
Using messages inside format control string

Time:04-09

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);

CodePudding user response:

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("           
  • Related