Difference between different scanf formats [duplicate] - c

This question already has answers here:
whitespace in the format string (scanf)
(4 answers)
Closed 6 years ago.
I'm currently prepping myself for programming school by going through the textbook. There's this particular question which I don't understand and the textbook doesn't give the answer.
PS: I've learned some C++/C# online, but never go through proper-taught programming classes, so I'm struggling with some of the concepts.
Q: For each of the following pairs of scanf format strings, indicate
whether or not the two strings are equivalent. If they are not, show
how they can be distinguished.
A) "%d" versus " %d"
B) "%d-%d-%d" versus "%d -%d -%d"
C) "%f" versus "%f "
D) "%f,%f" versus "%f, %f"
First off, I don't even understand what the question is asking. What does the textbook mean by whether or not the 2 strings are 'equivalent'?
If they are, could someone explain the differences and possibly show me how they can be distinguished?

Let us try A first: "%d" versus " %d", they are equivalent format strings for scanf().
" " will do the following. It never fails.
1) Scan and discard (skip) optional white-space.
2) After reading a non-white-space or end-of-file, if not (EOF), the last character read is put back into stdin.
"%d" itself will attempt 3 things (It can fail)
1) Scan and discard (skip) optional white-space.
2) Scan and convert numeric text representing a decimal integer.
3) After reading a non-numeric text or end-of-file, if not (EOF), the last character read is put back into stdin.
" %d" does both the above. It is the same result of just doing the 2nd with "%d".
With *scanf() specifiers note:
Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. C11 §7.21.6.2 8
B, C, D differences?
Mouse over for hint 1:
A " " before a scanf() specifier, except the 3 noted above, is an equivalent scanf() format as without it.
Mouse over for hint 2:
Only 1 of 3 equivalent.
Mouse over for hint 3:
Consider inputs:
"123 -456-789"
"123.456 x" What is the next character to be read?
B) "%d-%d-%d" versus "%d -%d -%d"
C) "%f" versus "%f "
D) "%f,%f" versus "%f, %f"
Answer:
Awww, Do you really want to be given the answer?

From Wikipedia
whitespace: Any whitespace characters trigger a scan for zero or more whitespace characters. The number and type of whitespace characters do not need to match in either direction.
scanf is about keep consuming the input and get the thing you care about. The normal char in format string means it consumes the exactly same char, and do nothing else. %d, %f could skip the leading whitespace. So, sum it up, we got:
A, it is the same, because %d skip the leading space
B, %d-%d-%d is pretty strict, it reads an integer after exactly - and then another integer and so on, so it reads 1-2-3 well, 1- 2- 3 well, too, but it fails on 1 - 2 - 3. While on the other hand, %d -%d -%d first skip spaces, read an integer, skip spaces, expect char-, then skip spaces again, and so on...
C, trailing spaces does not make a difference
D, it is the same, because %f skip leading spaces, too
So the answer would be B.

Related

C language: scanf and sscanf expressions

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

Wanting scanf to proceed without all parameters filled [duplicate]

This question already has answers here:
How to make that scanf is optionally ignoring one of its conversion specifiers?
(4 answers)
Closed 2 years ago.
I want to have a scanf function that allows the user to input up to four integers separated by spaces but still run if only 2 integers are put in.
scanf("%d %d %d %d", &command, &num_one, &num_two, &num_three);
scanf does exactly that. It returns the number of successful conversions it performed. If it cannot perform a conversion (or cannot match a literal character), it stops reading precisely at that point.
You should always check its return value, even if the examples you are copying don't do that.
What scanf doesn't guarantee is that the values converted are separated by spaces. They might be separated by newlines. If you want a newline character to stop the scan, you need to read the line using something like fgets (or, better if possible, the Posix getline function), and then call sscanf on the line which was read.
You could also force scanf to stop at the end of the line by using %*[ \t] instead of to separate the %ds, which will only match space and tab characters. (The * causes scanf to not try to save the matched string, and also to not count the conversion in its return count.) But that will run you into the other problem with scanf: if there is garbage in the line, you normally want to continue reading with the next line. The getline/sscanf solution will do that for you. If you use scanf, you'll need to manually flush the rest of the input line, which requires calling fgets or getline anyway.
And while I'm at it, note that there is no difference between scanf("%d %d %d %d", ...) and scanf("%d%d%d%d", ...), because %d, like all scanf conversions other than %c, %[ and %%, skips leading whitespace.

Integer taken with scanf() does not get its intended value in C

Here is my C code:
#include "stdio.h"
int main()
{
int minx, x;
printf("Enter two ints: ");
scanf( "%d-%d", &minx, &x);
printf("You wrote: minx is %d x is %d", minx, x);
}
When the input is 5-3 or 5- 3 the output is You wrote: minx is 5 x is 3 which makes sense. However, when the input is 5 -3 or 5 - 3 or 6 -4 the output is You wrote: minx is 5 x is 8. I expected - to skip white spaces, so I expected minx to be 5 and x to be 3,6 and 4 for the other input. This also happens when - in %d-%d changed to ?, *, + even with the same inputs. I know it is probably because of that space after the first int. Here it says only three format specifiers do not skip white space — Whitespace before %c specification in the format specifier of scanf function in C. Did I get this wrong? Why - does not skip leading space here? What is the actual problem here and what is the cause of it? Why is it 8? What other operators or chars can lead to similar problems?
Let's look at your scanf format specifier "%d-%d" in detail:
%d skip whitespace if necessary, then read an integer
- match a literal '-' character
%d skip whitespace if necessary, then read an integer
So the inputs 5-3 and 5- 3 both work just fine. But when the input is 5 -3 (or anything else with a space before the -) the parse fails, because scanf does not immediately see the - it expects.
If that's not what you expected, or not what you want, or if that doesn't make sense, or if that's not how you'd like scanf to work, I'm afraid that's too bad: scanf works the way it works.
How can you fix this? That depends in part on why you included the - character in your format string in the first place.
You can use %d%d or %d %d, which will simply read two integers (separated by at least one whitespace character). If there's a - character preceding the second integer, that integer will be read as negative.
You can use %d -%d, which will skip (arbitrary) whitespace before it tries to match the - character.
You can use two separate scanf calls.
If you do continue to use scanf, you really need to check its return value so that your program can detect the case that the expected inputs were not matched.
Finally, you can use something other than scanf.
My recommendation to you depends on the ultimate purpose of this program.
If it's just for learning, then minimize the amount of time you spend fussing with the way your program does input at all. For example, if you need to read an integer, use one %d. As long as you can get the numbers you need into your program (so that you can test the rest of your program), you're fine. If there are things you can type that cause scanf to get confused, just don't worry about it, just don't type those things. Don't try to do anything fancy — that's not what scanf is for.
If this is a "real" program, that does have to accept arbitrary user input, or input with a specific syntax (like with that - in the right place), and if you need to deal gracefully with incorrect input, printing appropriate errors, and not reading the wrong values or getting confused — then run, do not walk, away from scanf, and never use it again. It is effectively impossible to write a program that performs high-quality input using scanf. Not even a C expert can do it. It's simply not worth it. You will spend five times as long, and get an inferior result, than if you simply abandoned scanf and read your input a line at a time using fgets or the like, then parsed the input line (perhaps even using sscanf — but again, check its return value).
Addendum: It's true, all format specifiers — with three exceptions (%c, %[…] scan sets, and %n) — skip whitespace before beginning their work. But format specifiers are things that begin with %. Literal characters in the format string must match exactly, and there's no implicit whitespace skipping for them. If you want to skip whitespace at spots in the input other than before the % format specifiers that do it, you can include a literal whitespace character (usually a single space) in your format string.
You need to check the return value of scanf. You should be doing this, in general, for every library function you call that has a return value.
In your case, scanf would return 2 (meaning two output parameters were set) for valid input. If you get any other return value, the input does not match the format you specified, and you should ignore the contents of your output parameters minx and x.

Program is skipping 2nd switch [duplicate]

This question already has answers here:
How to read / parse input in C? The FAQ
(1 answer)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 years ago.
I am facing problem regarding scanf in C. When I run this:
char c;
int a, b;
scanf("%d", &a);
scanf("%c", &c);
scanf("%d", &b);
Then first two scanf were working properly but 3rd one is skipped completely. I searched different posts in this forum about this problem and found a lot of information, but want to know something else.
I already found that the easiest solution would be:
scanf("%d %c %d", &a, &c, &b);
And another solution could be using:
getchar();
And I also found that the reason behind my problem is it writes an extra new line character \n to the buffer, that's why the 3rd one was skipped. But for further researching I found that when I use another scanf of char type after 2nd scanf then it works. That means, in my case the problem occurs if I take input of any integer type after char type. Again I have seen many others were problem having opposite case, they couldn't take input of char after integer. Now I want to be clarified about the exact schemes those are supported in C for scanf that is when I will face similar problems and why char can be scanned after char but integer can't. Thanks to all.
The "%d" format specifier includes skipping leading white-space. The "%c" format doesn't have that, it reads the next character in the input buffer no matter what kind of character it is.
If you want to skip leading white-space using the "%c" format, you need to tell scanf explicitly to do so with a space in the format string, like e.g. " %c".
This is the same problem as "not able to take char after an int", with a small twist.
Recall that when you read an int from an input using scanf with %d format specifier, the characters representing the number are consumed, but the separator following it is not. Consider a buffer that looks like this (I use underscores to show spaces):
char 9 8 _ x _ 7 6
- - - - - - -
position 0 1 2 3 4 5 6
Before the first scanf call your buffer is positioned at zero.
After the first call your buffer is positioned at 2.
After the second call your buffer is positioned at 3, because space is read for your char
Now the problem becomes clear: the third scanf tries to read an int, but the buffer is at x, so the read is not possible.

Why this C scanf is automatically skipped? [duplicate]

This question already has answers here:
How to read / parse input in C? The FAQ
(1 answer)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 years ago.
I am facing problem regarding scanf in C. When I run this:
char c;
int a, b;
scanf("%d", &a);
scanf("%c", &c);
scanf("%d", &b);
Then first two scanf were working properly but 3rd one is skipped completely. I searched different posts in this forum about this problem and found a lot of information, but want to know something else.
I already found that the easiest solution would be:
scanf("%d %c %d", &a, &c, &b);
And another solution could be using:
getchar();
And I also found that the reason behind my problem is it writes an extra new line character \n to the buffer, that's why the 3rd one was skipped. But for further researching I found that when I use another scanf of char type after 2nd scanf then it works. That means, in my case the problem occurs if I take input of any integer type after char type. Again I have seen many others were problem having opposite case, they couldn't take input of char after integer. Now I want to be clarified about the exact schemes those are supported in C for scanf that is when I will face similar problems and why char can be scanned after char but integer can't. Thanks to all.
The "%d" format specifier includes skipping leading white-space. The "%c" format doesn't have that, it reads the next character in the input buffer no matter what kind of character it is.
If you want to skip leading white-space using the "%c" format, you need to tell scanf explicitly to do so with a space in the format string, like e.g. " %c".
This is the same problem as "not able to take char after an int", with a small twist.
Recall that when you read an int from an input using scanf with %d format specifier, the characters representing the number are consumed, but the separator following it is not. Consider a buffer that looks like this (I use underscores to show spaces):
char 9 8 _ x _ 7 6
- - - - - - -
position 0 1 2 3 4 5 6
Before the first scanf call your buffer is positioned at zero.
After the first call your buffer is positioned at 2.
After the second call your buffer is positioned at 3, because space is read for your char
Now the problem becomes clear: the third scanf tries to read an int, but the buffer is at x, so the read is not possible.

Resources