I have a case where I\'m not sure if I\'ll get enough input for sscanf
. Can I safely assume that sscanf
won\'t mess with any argument that it doesn
The only reference I can find in C11 is for fscanf
, 7.21.6.2/10, which says:
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.
I interpret "is placed in" as "is assigned to", and I'd say with reasonable confidence that this means that an assignment happens if and only if there's a match, otherwise the recipient variable isn't touched.
Note that all of the variable argument are evaluated before the function call, of course; this has nothing to do with scanf
.
You are completely safe.
In C11, 7.21.6.7 The sscanf function
2. ...Reaching the end of the string is equivalent to encountering end-of-file for the fscanf function.
7.21.6.2 The fscanf function says,
4. The fscanf function executes each directive of the format in turn. When all directives have been executed, or if a directive fails (as detailed below), the function returns. Failures are described as input failures (due to the occurrence of an encoding error or the unavailability of input characters), or matching failures (due to inappropriate input).
16. The fscanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed.Otherwise, the function returns the number of input items assigned.
Your case is an input failure.
7.21.6.2, para 21:
EXAMPLE 4 In:
#include <stdio.h>
/* ... */
int d1, d2, n1, n2, i;
i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
the value 123 is assigned to d1 and the value 3 to n1. Because %n can never get
an input failure, the value of 3 is also assigned to n2. The value of d2 is not
affected. The value 1 is assigned to i.
That's the closest thing that I can find to an assurance that a parameter will not be modified if no corresponding input is available.
From http://pubs.opengroup.org/onlinepubs/009695399/functions/scanf.html:
... If the format is exhausted while arguments remain, the excess arguments shall be evaluated but otherwise ignored.
Edit: As Kerrek correctly noticed, the above statement does not apply here.
It is about sscanf("1", "%d", &a, &b, &c)
, and not about
sscanf("1", "%d %d %d", &a, &b, &c)
as in the question.
Guranteed. sscanf() will not assign any thing to the extra parameters. You can avoid checking for the return value for this
The C library function int sscanf(const char *str, const char *format, ...)
reads formatted input from a string instead of stdin
On success, the function returns the number of items in the argument list successfully filled.
On failure, This count can match the expected number of items or be less (even zero) in the case of a matching failure.
In the case of an input failure before any data could be successfully interpreted, EOF is returned.
If there are too many arguments for the conversion specifications, the extra arguments are evaluated but otherwise ignored.
The results are not defined if there are not enough arguments for the conversion specifications.
All three functions (fscanf, scanf, and sscanf) return
the number of input items that were successfully matched and assigned. The return value does not include conversions that were performed but not assigned (for example, suppressed assignments).
If you check with the Number of Items count That might become false in some cases.