Is `sscanf` guaranteed not to change arguments tha

2019-02-17 14:04发布

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't find?

For example, in this program:

#include <stdio.h>

int main(int argc, char** argv) {
    int a = 0, b = 0, c = 0;
    sscanf("1 2", "%d %d %d", &a, &b, &c);
    printf("%d %d %d\n", a, b, c);
    return 0;
}

The output is:

1 2 0

So, it read two of the three numbers, and didn't mess with the last one. Can I safely assume that all compilers and standard libraries will also leave the last argument alone in this case, or do I need to do something like this:

int main(int argc, char** argv) {
    int a = 0, b = 0, c = 0;
    if (sscanf("1 2", "%d %d %d", &a, &b, &c) != 3) {
        c = 0;
    }
    printf("%d %d %d\n", a, b, c);
    return 0;
}

标签: c scanf
5条回答
劳资没心,怎么记你
2楼-- · 2019-02-17 14:39

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.

查看更多
Luminary・发光体
3楼-- · 2019-02-17 14:39

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.

查看更多
冷血范
4楼-- · 2019-02-17 14:42

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.

查看更多
forever°为你锁心
5楼-- · 2019-02-17 14:53

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.

查看更多
该账号已被封号
6楼-- · 2019-02-17 14:58

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.

查看更多
登录 后发表回答