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?
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.
"%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.
I've encountered a expressions that go something like this inside scanf and sscanf arguments:
sscanf(buffer, "%d,%100[^,]%*c%f", destination_pointer)
or
scanf("\n%99s", destination);
What is the correct way of interpreting these? I know what things like "%s %c %d" are, and also that the %100 or generally "%number" is the size of the input to be read. But what about the rest? All I can find are basic examples, nothing near this complex. Is there any reference guide?
What is the correct way to interpreted these?
sscanf(buffer, "%d,%100[^,]%*c%f", destinantion_pointer)
Is an invalid call. There are 3 conversion specifiers that need an argument - %d, %[], %f. That means exactly 3 arguments after formatting string are needed, but only one destinantion_pointer is provided.
%d - ignore any whitespace characters, read an int in base 10
, - read a comma
%100[^,] - read maximum number of 100 characters that are not a comma. Maximum up to 101 bytes (100 characters + null byte) are stored in destination buffer.
%[set] - reads characters in the set
%[^set] - reads characters that are not in the set
%*c - ignore one character (a comma, because %100[^,] reads up until a comma, or the string has ended, which would make scanf return here). Note - ignoring the result of conversion with * makes scanf not increment the return value in the case reading was successful.
%f - ignore any whitespace characters, read a float (in any format - decimal, scientific or hexadecimal)
scanf("\n%99s", destinantion);
\n - read (and ignore) any number of whitespace characters (whitespace, means anything for that isspace() returns nonzero, so either space, form feed, line feed, carriage return, tab or vertical tab)
%99s - ignore any leading whitespace characters (\n in front of it is useless...), then read up to 99 characters that are not whitespaces (the resulting buffer has to be at least 100 bytes long).
What is the specifier %[^s] used for?
s is a variable.
In which cases can I use this specifier?
The %[ format specifier to scanf will match a sequence of characters matching those that are listed between [ and ]. If the first character is ^, then it matches characters excluding those characters.
In your case %[^s] means "match any character besides the characters 's'. s is not a variable in this case.
I am very new to C programming. My sir gave this code to find the maximum of n numbers. When I do as Sir says things are perfect i.e Write a number when the line - Type the number of numbers and write numbers in a row like 7 8 9 10 when Type the numbers pop up.
#include <stdio.h>
main()
{
int n, max, number, i;
printf("Type the number of numbers");
scanf("%d", &n);
if(n>0)
{
printf("Type the numbers");
scanf("%d",&number);
max=number;
for(i=1; i<n; i++)
{
scanf("%d", &number);
if(number>max)
max=number;
}
printf("MAX=%d \n", max);
}
}
But if I write suppose - 5 8 9 10 7 6 - then the program understands it like --
It puts n = 5 then puts number = 8 then loop executes number changes to 9 then number changes to 10 till 6 and then gves max.
So how is scanf working here? It takes digit individually although they are written in a row with spaces?
From the horse's mouth:
7.21.6.2 The fscanf function
...7 A directive that is a conversion specification defines a set of matching input sequences, as
described below for each specifier. A conversion specification is executed in the
following steps:
8 Input white-space characters (as specified by the isspace function) are skipped, unless
the specification includes a [, c, or n specifier.284)
9 An input item is read from the stream, unless the specification includes an n specifier. An
input item is defined as the longest sequence of input characters which does not exceed
any specified field width and which is, or is a prefix of, a matching input sequence.285)
The first character, if any, after the input item remains unread. If the length of the input
item is zero, the execution of the directive fails; this condition is a matching failure unless
end-of-file, an encoding error, or a read error prevented input from the stream, in which
case it is an input failure.
10 Except in the case of a % specifier, the input item (or, in the case of a %n directive, the
count of input characters) is converted to a type appropriate to the conversion specifier. If
the input item is not a matching sequence, the execution of the directive fails: this
condition is a matching failure. Unless assignment suppression was indicated by a *, the
result of the conversion is placed in the object pointed to by the first argument following
the format argument that has not already received a conversion result. If this object
does not have an appropriate type, or if the result of the conversion cannot be represented
in the object, the behavior is undefined.
...
12 The conversion specifiers and their meanings are:
d Matches an optionally signed decimal integer, whose format is the same as
expected for the subject sequence of the strtol function with the value 10
for
the base argument. The corresponding argument shall be a pointer to
signed integer.
...
284) These white-space characters are not counted against a specified field width.
285) fscanf pushes back at most one input character onto the input stream. Therefore, some sequences
that are acceptable to strtod, strtol, etc., are unacceptable to fscanf.
The processing for scanf is exactly the same; the only difference is that scanf always reads from standard input.
Examples:
Suppose you type SpaceSpaceSpace123Enter in response to the first prompt; the input stream then contains the sequence {' ', ' ', ' ', '1', '2', '3', '\n'}. When you call scanf( "%d", &n );, scanf reads and discards the leading blank spaces, then reads and matches the sequence {'1', '2', '3'}, converts it to the integer value 123, and assigns the result to n. Since there was a successful conversion and assignment, scanf returns 1.
If the input stream contains the sequence {' ', ' ', ' ', '1', '2', '.', '3', '\n'}, scanf reads and discards the leading blanks, then reads and matches the sequence {'1', '2'}, converts it to the integer value 12, and assigns the result to n. The input stream will still contain {'.', '3', '\n'}. Since there was a successful conversion and assignment, scanf will return 1.
If the input stream contains the sequence {'.', '3', '\n'}, then there is no matching sequence of characters ('.' is not a valid character in a decimal integer). scanf will leave the . unread and leave the value of n unchanged. Since there was not a successful conversion and assignment, scanf returns 0 to indicate a matching failure.
If an end-of-file is signaled on the input stream before any matching characters have been read, or if there's some other input error, scanf does not assign any new value to n and returns EOF to indicate an input failure.
The "%d" in scanf("%d",&number); causes has 3 stages of scanning user text input into an int.
0 or more leading whites-space, like ' ', '\n', '\t' and some others are read and discarded.
Numeric text like "123", "-123", "+123" is read until until a non-numeric character is read. (or end-of-file, or a rare input error).
That non-numeric character is put back into stdin for subsequent input calls.
If step 2 is successful in reading at least 1 digit, the function returns 1. Good code checks the returned value.
if (scanf("%d",&number) != 1) Handle_UnexpectedInput();
The important thing is that '\n' is not so special with scanf("%d",&number);. It acts like a separator like another white-space or non-numeric text.
'\n' does cause the buffered stdin to accept the line of user input for processing by the various scanf() calls.
Here it is simplified explanation (not overly simplified, I hope) on how scanf works from a user point of view:
The arguments are divided in two parts:
The first part is a “format string”. The string is made of at least
one format specifier. In its simplest form a specifier begins with
% and it is followed by a letter that specifies the type of
variable you’re expecting (“%d” – I’m expecting an integer). The
number of specifiers must match the number of parameters and types
in the second part.
The second part is made of one or more addresses to locations memory
where the data you input will be stored. The pointed types must
match the specifiers.
When called, the function will repeat the following steps, starting with the first specifier and the first pointer, until the end of format string is detected:
Read and discard any white-space until a non-white-space character is found (white-space: space, tab, NL, at least);
Read characters up to first white-space or a character that do not match the expected input for current specifier;
Convert them to the type of current specifier and
Store the result in the location pointed by the current pointer.
There are three typical beginner mistakes which will result in undefined behavior (crash, most likely):
You forget the address-of operator &.
The specifier and the type do not match.
The number of specifiers do not match the number of pointers.
int d;
scanf( "%d", d ); // no &
scanf_s( "%s", &d ); // s do not match int
scanf_s( "%d%d", &d ); // too many specifiers
when you press the keyboard, you are filling a buffer on your computer. the scanf will read the amount of consecutive data untill it hit a space, so "1234 43", on the code scanf("%d") you are saying "read one number", and 1234 is one number, that's what it will read.
But if you have a loop that will execute that scanf again, the number "43" is currently in the reading buffer, and scanf will read it without stopping.
The manual for scanf doesn't explains that and it's a bit confusing for a newcomer to understand why the application is not stopping there to read a new number.
Let me explain as simply as possible...
scanf reads bytes from the standard input stream stdin.
Let's say the input you give is "23 67 21 99\n" (The \n is from when you pressed Enter).
Then each next call to scanf will start reading from this input buffer and it will interpret what is sees as whatever you tell it ("%d", etc) while separating inputs by an empty character. This could be a new line, a space, a tab, etc.
While there are still bytes to be read, scanf will not wait for you to input. That is what is happening here.
let's keep it simple. I assume you don't know anything about buffer or stdin.
scanf is used to take input from user. Whenever you type a number and press 'space' or ' enter' on keyboard the number is entered into program for further purposes. When you type scanf("%d",&n); it mean take integer input from the user and store it on the address of variable n.
Can anyone suggest what is the meaning of a in the following call to scanf?
scanf("%d a %f",&i,&f)
Characters preceded by a '%' in a call to scanf represent variables.
For instance %d represents an integer variable whereas %f represents a floating-point variable.
Characters which are not preceded by a % (or a \, which indicates an escape sequence) are taken literally, so, in your case, the scanf string "%d a %f" would match "233 a 4.5" but would not match "233 b 4.5".
(To be more accurate, a whitespace character matches any contiguous sequence of whitespace characters.)
scanf("%d a %f",&i,&f)
Means you have to type in data , in this format 25 a 33.3
Then when you print it using
printf("i=%d f=%f",i,f);
and then you get the output as
i = 25 , f = 33.3
You are not getting the value of the variable f because of the & in the line scanf("%d a %f",&i,&f)
The & means you are getting the address of the variable f in the memory. You should remove the '&'s to get the actual value of the variables.
And for the a:
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.
Which means you are formatting the input as so:
type in a decimal integer(%d)
then a space
then the character 'a'
another space
then the floating point number(%f).
Reference: http://www.cplusplus.com/reference/cstdio/scanf/