The man page states that the signature of sscanf
is
sscanf(const char *restrict s, const char *restrict format, ...);
I have seen an answer on SO where a function in which sscanf
is used like this to check if an input was an integer.
bool is_int(char const* s) {
int n;
int i;
return sscanf(s, "%d %n", &i, &n) == 1 && !s[n];
}
Looking at !s[n]
it seems to suggest that we check if sscanf
scanned the character sequence until the termination character
. So I assume n
stands for the index where sscanf will be in the string s
when the function ends.
But what about the variable i
? What does it mean?
Edit:
To be more explicit: I see the signature of sscanf
wants a pointer of type char *
as first parameter. A format specifier as seconf parameter so it knows how to parse the character sequence and as much variables as conversion specifiers as next parameters. I understand now that i
is for holding the parsed integer.
Since there is only one format specifier, I tried to deduce the function of n
.
Is my assumption above for n
correct?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…