...
This non-compliant code example converts the string stored in the static array buff to a signed integer value using the atoi() function.
| Code Block |
|---|
char buff [25]; int int_varsi; fgetsif (buff,argc sizeof> buff, stdin); int_var1) { si = atoi(buffargv[1]); } |
The atoi(), atol(), and atoll() functions convert the initial portion of astring pointed to int, long int, and long long int representation, respectively. Except for the behavior on error, they are equivalent to
...
This non-compliant example uses the sscanf() function to convert a string to an integer. The sscanf() function has the same problems as atoi().
| Code Block |
|---|
char buff [25]; int int_varsi; fgetsif (buff,argc sizeof> buff, stdin); sscanf(1) { sscanf(argv[1], "%d", buff, &int_var&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.
...