sscanf behaviour / return value

In case sscanf has successfully read %d and nothing else, it would return 1 (one parameter has been assigned). If there were characters before a number, it would return 0 (no paramters were assigned since it was required to find an integer first which was not present). If there was an integer with additional characters, it would return 2 as it was able to assign both parameters.


Your sscanf(string, " %d %c") will return EOF, 0,1 or 2:

2: If your input matches the following
[Optional spaces][decimal digits1][Optional spaces][any character][following data not read]

1: If your input failed above but matched the following
[Optional spaces][decimal digits1][Optional spaces][no more data available]

0: If your input, after white-space and an optional sign, did not find a digit: examples: "z" or "-".

EOF: If input was empty "" or only white-space2.


1 The decimal digits may be preceded by a single sign character: + or -.

2 Or rare input error.


You can always check what a function returns by putting it in a printf statement like below :

printf("%d",sscanf(string, " %d %c", &n, &c));

This will probably clear your doubt by printing out the return value of sscanf on your terminal.

Also you can check this out : cplusplus : sscanf

Hope that helped :)