12

I read that atoi() is deprecated and that it is equivalent to:

(int)strtol(token_start, (char **)NULL, 10);

Does that mean I should use the above instead of atoi(chr) or is it just saying they are equivalent?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user105033
  • 18,800
  • 19
  • 58
  • 69
  • 3
    My problem with `atoi`: `assert( atoi("0") != atoi(!blah") )` – sbi Sep 28 '09 at 18:43
  • 3
    `atoi` is poor because it does almost no well-defined error detection, but even when using `strtol`, properly detecting (and classifying) errors is surprisingly difficult, and none of the answers here really addresses that. See the answers at [this question](https://stackoverflow.com/questions/36074422/why-cant-you-just-check-if-errno-is-equal-to-erange/) for some guidance. – Steve Summit Jun 24 '18 at 12:06

5 Answers5

22

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."

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 3
    (Yes I know this question is terribly old but the accepted answer is incorrect, had to be clarified) – Lundin Feb 24 '15 at 16:05
  • 2
    "If the value of the result cannot be represented, the behavior is undefined". Surely this make the function completely unusable, and is sufficient reason to consider the function effectively deprecated. – William Pursell Jun 13 '17 at 13:09
  • 1
    @WilliamPursell Indeed. Give the C standard committee a couple of decades more and they might realize. For example, it took them 22 years to remove `gets`. The very epitome of efficiency and initiative. Compare this with the MISRA-C safe subset standard, which banned all use of the ato* functions as early as 1998. – Lundin Jun 13 '17 at 14:02
8

It does say on Apple's Mac OS X Manual Page for atoi(3) (and in the BSD man pages too) that atoi has been deprecated.

The atoi() function has been deprecated by strtol() and should not be used in new code.

I would use the strtol() equivalent just for that reason, but i doubt you have to worry about atoi() being removed.

from http://www.codecogs.com/library/computing/c/stdlib.h/atoi.php Implementation Notes

* The atoi function is not thread-safe and also not async-cancel safe.
* The atoi function has been deprecated by strtol and should not be used in new code.

John Boker
  • 82,559
  • 17
  • 97
  • 130
  • 3
    Doesn't it depend only on the internal implementation of the `atoi` whether it's thread-safe or not? If not, then what makes it not thread-safe on the outside? – SasQ Sep 19 '11 at 09:30
  • 4
    They must've tried really hard to make it not thread safe. The naive implementation sure is. – Per Johansson Sep 19 '11 at 11:33
  • It is important to note that not every compiler supports sto* functions yet, like Android NDK. See this question for more: http://stackoverflow.com/questions/15499070/stdstring-operations-i-e-stol-stoi-not-found-ndk8d – catalyst294 Aug 27 '14 at 15:34
  • 1
    glibc man page for atoi says: "The atoi(), atol(), and atoll() functions are thread-safe with exceptions. These functions can be safely used in multithreaded applications, as long as setlocale(3) is not called to change the locale during their execution." – dequis Nov 20 '14 at 08:40
  • 2
    This answer, or rather the source, is incorrect/irrelevant. Whether or not a certain function is thread-safe indeed depends on the compiler implementation. I don't see why it wouldn't be. atoi is no different from strol functions in this regard. More so, atoi is most certainly not deprecated. It wasn't in 2009 and it isn't now either, in the C11 standard. – Lundin Feb 24 '15 at 15:58
  • @Lundin i cannot delete this answer because it is the accepted one, so i updated it. I found that the BSD and OSX man pages do say it's deprecated, although i don't know what the original question is referring to. – John Boker Feb 24 '15 at 16:37
4

The description of atoi() has one very important point in relation to the similarities/differences to strtol()

> ... The call atoi(str) shall be equivalent to:
> (int) strtol(str, (char **)NULL, 10)
> except that the handling of errors may differ.

Try this for fun:

const char *buf = "forty two";
int t1 = atoi(buf);             /* detect errors? */
int t2 = strtol(buf, NULL, 10); /* detect errors? */

pmg
  • 106,608
  • 13
  • 126
  • 198
  • I've tried your code on my compiler (GCC) and both gave me `0`, so I don't see any difference :| – SasQ Sep 19 '11 at 09:36
  • Hmmm, my bad! Thanks @SasQ. I thought `strtol` had to set `errno` on error, but in the specific case of my test code above, it doesn't. – pmg Sep 19 '11 at 10:17
  • @pmg Your test code doesn't seem to check `errno` at all. – Dan Bechard Jun 30 '16 at 01:25
3

No, you shouldn't use the above instead of atoi.

You should actually check the error information that strtol makes available:

i = atoi(s);

should be replaced by

char* stopped;
i = (int)strtol(s, &stopped, 10);
if (*stopped) { /* handle error */ }
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
-3

it means that at one point in time atoi will not be available anymore. So start changing your code now

Toad
  • 15,593
  • 16
  • 82
  • 128
  • I get that part, but was wondering if atoi's implementation just called the code above. – user105033 Sep 28 '09 at 18:28
  • 6
    This is incorrect, citation needed. Nothing in the current C standard indicates that atoi is deprecated. And this is 6 years after this answer was made, with C11 being the active standard. – Lundin Feb 24 '15 at 15:59