Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about format string in scanf function

Tags:

c

scanf

For each of the following pairs of scanf format strings, indicate whether or not the two strings are equivalent. If they're not, show how they can be distinguished:

(b) "%d-%d-%d" versus "%d -%d -%d"

So in this case, my answer was that they were not equivalent. Because non-white-space characters except conversion specifier which start with %, cannot be preceded by spaces, it will not match with the non-white-space character. So in the first case, no spaces will be allowed after the first and second integer, while in the second case, any number of spaces will be allowed after the first 2 integers.

But I saw that the book had a different answer. It said that they were both equivalent to each other. Is this the mistake of the book? Or am I just wrong with the concept of format string in the scanf function?

like image 395
kori the newbie Avatar asked Mar 07 '21 13:03

kori the newbie


People also ask

What is format-string in scanf function?

A scanf format string (scan formatted) is a control parameter used in various functions to specify the layout of an input string. The functions can then divide the string and translate into values of appropriate data types. String scanning functions are often supplied in standard libraries.

What is the problem with scanf () to read a string?

Explanation: The problem with the above code is scanf() reads an integer and leaves a newline character in the buffer. So fgets() only reads newline and the string “test” is ignored by the program.

Which format specifier we used in scanf () to scan a string?

Explanation: Since both the input values are integers and the format specifier in the scanf() function is '%d', thus input values are read and scanf() function returns the number of values read.

What types of data can the scanf () function read?

The scanf() function reads format-string from left to right. Characters outside of format specifications are expected to match the sequence of characters in stdin ; the matched characters in stdin are scanned but not stored. If a character in stdin conflicts with format-string, scanf() ends.


2 Answers

The book is wrong. As per the specification of the scanf():

  • Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).
  • Non-whitespace character, except format specifier (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.

So in first case when scanf arrives to the %d and gets the input, next is the - which means that scanf will expect next in the stream to see the non-whitespae character - and not any other whitespace character. So the legal input is 1- 2, but not 1 -2

In the second case, after first %d, scanf will allow the whitespace and than will arrive to non-whitespace, so it will allow the input 1 - 2 by the above definitions.

like image 79
jordanvrtanoski Avatar answered Dec 04 '22 04:12

jordanvrtanoski


"%d-%d-%d" differs from "%d -%d -%d" and the difference has nothing to do with "%d".


Format "-" scans over input "-" and stops on the first space of input " -".

Format " -" scans over inputs "-" and " -" as the " " in the format matches 0 or more white-space characters in the input.

A directive composed of white-space character(s) is executed by reading input up to the first nonwhite-space character (which remains unread), or until no more characters can be read. The directive never fails. C17dr § 7.21.6.2 5


Had the question been: "%d-%d-%d" versus "%d- %d- %d",

These 2 are functionally identical.
We would need to dive input arcane stdin input errors to divine a potential difference.

like image 31
chux - Reinstate Monica Avatar answered Dec 04 '22 06:12

chux - Reinstate Monica