You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Formatted input functions such as scanf(), fscanf(), vscanf(), and vfscanf() can be used to read string data from stdin or (in the cases of fscanf() and vfscanf()) other input stream. These functions work fine for valid integer values but lack robust error handling for invalid values.

Instead of these functions, try inputing the value as a string and then converting it to an integer valoue using strtol() or a related function INT00-A.

Non-Compliant Example

This non-compliant example uses the scanf() function to read a string from stdin and covert it to an integer value. The scanf() and fscanf() functions have undefined behavior if the value of the result of this operation cannot be represented as an integer.

int si;

scanf("%d", &si);

Compliant Solution

The following compliant example uses strtol() to input an integer value and provides error checking to make sure that the value is a valid integer in the range of int.

char buff [25];
char *end_ptr;
long long_var;
int int_var;

fgets(buff, sizeof buff, stdin);

errno = 0;

long_var = strtol(buff, &end_ptr, 0);

if (ERANGE == errno) {
  puts("number out of range\n");
}
else if (long_var > INT_MAX) {
  printf("%ld too large!\n", long_var);
}
else if (long_var < INT_MIN) {
  printf("%ld too small!\n", long_var);
}
else if (end_ptr == buff) {
  printf("not valid numeric input\n");
}
else {
  int_var = (int)long_var;
}

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.

References

  • No labels