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?
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.
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.
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.
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.
The book is wrong. As per the specification of the scanf()
:
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.
"%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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With