If you do not know when you write the code, the number and type of arguments, sscanf() cannot safely do what you are trying to do.
Passing 50 arguments to sscanf() in order (arguments that are not consumed by the format string are evaluated but ignored otherwise), but the arguments corresponding to the format string must be of the expected type after the move; otherwise, the behavior is undefined. Therefore, if you want to determine whether you can scan a string using "%d" or "%f" , you cannot safely do this with a single call to sscanf() . (You could probably get away with passing void* , which points to a sufficiently large buffer, but the behavior is still undefined.)
Another nasty problem with sscanf() is that it does not handle numeric overflow. It:
char *s = "9999999999999999999999999"; int n; int result = sscanf(s, "%d", &n); printf("result = %d, n = %d\n", result, n);
has undefined behavior (provided that 99999999999999999999999999999 is too large to store in int ).
Something you could do is find an open source sscanf implementation and modify it so that it just checks the string in format without saving anything. (Working with the license for implementation is left as an exercise.) This makes sense if you find sscanf style strings that are especially convenient for your problem. Otherwise, regular expressions are probably suitable (not in the C standard, but it's easy enough to find an implementation).
source share