...
Code Block |
---|
char buff [25]; char *end_ptr; long long_varsl; int int_varsi; fgets(buff, sizeof buff, stdin); errno = 0; long_varsl = strtol(buff, &end_ptr, 0); if (ERANGE == errno) { puts("number out of range\n"); } else if (long_varsl > INT_MAX) { printf("%ld too large!\n", long_varsl); } else if (long_varsl < INT_MIN) { printf("%ld too small!\n", long_varsl); } else if (end_ptr == buff) { printfputs("not valid numeric input\n"); } else { int_varsi = (int)long_varsl; } |
If you are attempting to convert a string to a smaller interger type (int
, short
, or signed char
), then you only need test the result against the limits for that type. The tests do nothing if the smaller type happens to have the same size and representation on a particular compiler.
...