Heh, and I hadn't realised that gcc had this sneaky addition over the standard.
However, it seems to be almost completely useless.
int nr, items;
printf("Enter number: ");
fgets(buf, sizeof(buf), stdin);
errno = 0;
items = sscanf(buf, "%d%c", &nr, &ch);
if (items == 2 && ch == '\n')
if (errno != 0)
perror("Number out of range, try again");
puts("Not a number, try again");
if (buf[strlen(buf) - 1] != '\n')
while ((ch = getchar()) != '\n' && ch != EOF)
printf("The number is %d\n", nr);
It only seems to generate ERANGE if the number input exceeds the limits of long long int, regardless of the width of the datatype being converted at the moment.
Enter number: 2147483648
The number is -2147483648
Enter number: 9223372036854775808
Number out of range, try again: Numerical result out of range
Enter number: 9223372036854775807
The number is -1
If you have to read your data into the longest data type, then manually check the range to see if will fit in a smaller int, then you may as well use strtol() from the outset. It's the same PITA either way.