tags:

views:

1073

answers:

3

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?

A: 

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

Toad
I get that part, but was wondering if atoi's implementation just called the code above.
+6  A: 

from http://www.codecogs.com/reference/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
+2  A: 

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

> ... The behaviour is the same as
> strtol(nptr, (char **)NULL, 10);
> except that atoi() does not detect errors.

Try this for fun:

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