scanf doesn't check for numeric overflow, so it's inherently unsuited for getting numeric input.
The best way to get numeric input is to read it in as a string first with fgets or similar, and then use strtod to convert it to a double. If you want a float instead and strtof is available to you (C99 function), then you can use that instead. Otherwise it's not terrible hard to make your own function that uses strtod for the initial conversion.
Code:
#include <errno.h>
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#define INPUT_SIZE 80
extern int errno;
/*
* Returns 0 on failure and 1 on success.
*/
int
string_to_double(const char *s, double *result, char **end)
{
double d;
errno = 0;
d = strtod(s, end);
if (s == *end || ((d == 0 || d == HUGE_VAL) && errno == ERANGE))
{
return 0;
}
*result = d;
return 1;
}
/*
* Returns 0 on failure and 1 on success.
*/
int
string_to_float(const char *s, float *result, char **end)
{
double d;
if (!string_to_double(s, &d, end))
{
return 0;
}
if (d > FLT_MAX || d < FLT_MIN)
{
return 0;
}
*result = (float)d;
return 1;
}
int
main(void)
{
char input[INPUT_SIZE];
char *end;
float f;
if (!fgets(input, INPUT_SIZE, stdin))
{
puts("Invalid input!");
return EXIT_FAILURE;
}
if (!string_to_float(input, &f, &end))
{
puts("Invalid input!");
return EXIT_FAILURE;
}
printf("Number is: %f\n", f);
return EXIT_SUCCESS;
}