Say I have an invalid integer input to a char * where,
char *ch = "23 45"
using atoi(ch)
gives 23
as the converted output, ignoring the space and 45.
I'm trying to do testing on this input. What can I do to flag it as an invalid input?
Say I have an invalid integer input to a char * where,
char *ch = "23 45"
using atoi(ch)
gives 23
as the converted output, ignoring the space and 45.
I'm trying to do testing on this input. What can I do to flag it as an invalid input?
Either check the string before passing it to atoi()
or use strtol()
, though the latter will return long int
.
With strtol()
, you can check for errors:
RETURN VALUE
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow
occurs, strtol() returns LONG_MAX. In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and
LONG_MAX).
ERRORS
EINVAL (not in C99) The given base contains an unsupported value.
ERANGE The resulting value was out of range.
The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).
The lack of error detection is one of the main shortcomings of the atoi()
function. If that's something you need, then the basic answer is "don't use atoi()
."
The strtol()
function is a better alternative in pretty much every way. For your particular purpose, you can pass to it a pointer to a char *
, wherein it will record a pointer to the first character in the input that was not converted. If the whole string is successfully converted then a pointer to the string terminator will be stored, so you might write
_Bool is_valid_int(const char *to_test) {
// assumes to_test is not NULL
char *end;
long int result = strtol(to_test, &end, 10);
return (*to_test != '\0' && *end == '\0');
}