...
Code Block | ||
---|---|---|
| ||
char buff[25]; char *end_ptr; long sl; if (fgets(buff, sizeof(buff), stdin) == NULL) { puts("EOF or read error\n"); } else { errno = 0; sl = strtol(buff, &end_ptr, 10); if (ERANGE == errno) { puts("number out of range\n"); } else if (end_ptr == buff) { puts("not valid numeric input\n"); } else if ('\n' != *end_ptr && '\0' != *end_ptr) { puts("extra characters on input line\n"); } } |
Note that this solution treats any trailing characters, including white space characters, as an error condition.
...