atoi() — string to int

atoi is not deprecated, your source is incorrect. Nothing in the current C standard ISO 9899:2011 indicates this (see for example chapter 6.11 future language directions), nor anything in earlier standards.

As per the C standard, atoi is equivalent to strtol as follows, C11 7.22.1.2:

The atoi, atol, and atoll functions convert the initial portion of the
string pointed to by nptr to int, long int, and long long int
representation, respectively.

Except for the behavior on error, they are equivalent to

atoi: (int)strtol(nptr, (char **)NULL, 10)

atol: strtol(nptr, (char **)NULL, 10)

atoll: strtoll(nptr, (char **)NULL, 10)

strtol is preferred, as atoi invokes undefined behavior upon error. See 7.22.1 “If the value of the result cannot be represented, the
behavior is undefined.”

Leave a Comment