I need to validate the scanf parameters for example
if (scanf("%c,%f,%f", &ch, &p1, &p2) != 3)
// How can I tell which parameter failed?
// If I want to output message such as "Second parameter must be a real nubmer".
I need to validate the scanf parameters for example
if (scanf("%c,%f,%f", &ch, &p1, &p2) != 3)
// How can I tell which parameter failed?
// If I want to output message such as "Second parameter must be a real nubmer".
scanf
will stop scanning as soon as the first encountered symbol that does not match a format specifier. So if yourscanf
returns 1 then only the first format parameter was interpreted.Also please note that a return value smaller than the maximum number of successful parses may also indicate an error. In which case you should consult
ferror()
.