Alex Rivera | Logout

atoi() — string to int

Asked 2009-09-28T18:23:56.380
13

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?

c
Edit
Report

1 Answer

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? */

answered 2009-09-28T18:40:47.530

Your Answer