I want to confirm if a value returned from the scanf()
function is a floating number or not. How can I do that? My code is not running as it should if the wrong data types are supplied to the scanf()
function. Similarly, how would I confirm if a value returned is a character string or not?

- 730,956
- 141
- 904
- 1,278

- 9,558
- 19
- 72
- 111
-
All versions of `scanf` I have seen, returns an `int`. – leppie Apr 10 '12 at 06:44
-
1@Saurabh Going by your previous Qn, I think you want to know _whether scanf successfully scanned a float_ and not _whether its return value is a float_ as stated by your question? – Pavan Manjunath Apr 10 '12 at 06:51
-
3Perhaps the most important thing to know about the `*scanf()` family of functions is that if you scan a number whose value is outside the bounds of the appropriate type, the behavior is undefined. For example, if `scanf("%d")` tries to read `999999999999999999999`, anything can happen; there is no way to handle the error. There's no good workaround other than using something other than `*scanf()`, such as the `strto*()` functions. – Keith Thompson Apr 10 '12 at 07:07
-
1Even with the `strtol()` etc functions, testing for errors is not entirely trivial. Read the specification very carefully. Then reread it, several more times. – Jonathan Leffler Apr 10 '12 at 07:12
-
@PavanManjunath yes, I haven't yet learnt about any other functions besides printf and scanf. so I had difficulty in following the previous answers. All I could gather was that a function is not a fool-proof solution against all possible errors.. by this question, I wish to learn how to improve my previous code that contains scanf.. – KawaiKx Apr 10 '12 at 12:52
2 Answers
scanf()
et al return the number of successful conversions.
If you have:
scanf("%f", &f);
you should test:
if (scanf("%f", &f) == 1)
...all OK...
else
...EOF or conversion failure...
If you have several conversions, check that they all completed. If you're using %n
'conversions', they aren't counted.
Although scanf()
does return EOF on EOF, you should not test for that — you should always check primarily that you got the number of conversions you expected. For example, consider the buggy code:
while (scanf("%f %d %s", &f, &i, s) != EOF) // Here be BUGS!
...loop body...
If you type 3.14 x23 yes
, then you will have an infinite loop because scanf()
will return 1 on the first iteration (it successfully converted 3.14), and 0 thereafter (not EOF).
You might be OK with:
while ((rc = scanf("%f %d %s", &f, &i, s)) != EOF)
{
if (rc != 3)
...oops data problems...
else
...all OK...
}
Judging from previous questions, you should be looking at using fgets()
(or possibly POSIX getline()
) to read lines of data, and then using sscanf()
or even functions like strtol()
and strtod()
to read particular values from the line. If you use sscanf()
, the comments made above about checking the number of successful conversions still apply.
I don't use scanf()
in production code; it is just too damn hard to control properly. I regard it as almost suitable for beginning programs — except that it causes lots of confusion. On the whole, the best advice is 'stay clear of scanf()
and fscanf()
'. Note that that does not mean you have to stay clear of sscanf()
, though some caution is needed even with sscanf()
.

- 730,956
- 141
- 904
- 1,278
-
1Awesome answer! But I have a doubt: Will `char a; scanf("%c", &a);` ever return zero? – Spikatrix Mar 23 '17 at 12:03
-
1Taken in isolation as shown, `"%c"` should never return 0 because either there's a character to read so the result will be 1, or there's nothing left to read so the result will be EOF. You still need to check which of those two options applies, and using `if (scanf("%c", &a) == 1) { …got a char… }` is still correct, which is what is recommended in the main answer. – Jonathan Leffler Mar 23 '17 at 18:08
The return value of scanf
indicates the number of items successfully read and assigned to your variables, or EOF:
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.
If the int
returned by scanf
has a value less than the amount of input specifiers (or at least the specifiers which require an argument to be passed) in the format string, then you can assume something went wrong.
However, if you want to perform a more thorough validation of your floating point number, then you should consider using strtod()
. ie. for the input "1.23abc"
, scanf
would store 1.23
into your variable and tell you that everything went well, but with strtod
you can check if the last character converted is actually the end of the string being parsed.

- 18,014
- 6
- 40
- 44