I have been using atoi since an year ago and I got an issue in the recent days that the expression atoi("20") is giving a value of 0 as output. And when I went through google I came to know that it is deprecated and strtol have to be used. The interesting point I came to know is atoi internally uses strtol. So, how can the issue be solved when I change it to strtol?
-
5`atoi` sill works.. `atoi("20")` should return `20`. The problem is in _your_ code which you didn't show. [Edit] your question and show a [mcve]. Also read this: [ask] – Jabberwocky Mar 31 '20 at 08:34
-
1If you find that `atoi("20")` return 0, you should change your development tool or your computer... `atoi` has been deprecated by `strtol` because the latter handles the first character following the digits, which may matter. – Serge Ballesta Mar 31 '20 at 08:35
-
2Please provide a minimal reproducible example. The `atoi()` function might be deprecated, but it does still work just fine: [example](https://godbolt.org/z/_Ngaca) – Felix G Mar 31 '20 at 08:35
-
@Jabberwocky I know that I have to post the code. But the thing the project is big and if it helps I am using freertos for multitasking. – Saiteja Pabisetti Mar 31 '20 at 08:37
-
`atoi` has undefined behavior, `strtol` doesn't. See [Why shouldn't I use atoi()?](https://stackoverflow.com/q/17710018/995714) – phuclv Mar 31 '20 at 08:38
-
@SergeBallesta I think what you have told makes sense. But in the first iteration it works and in the second iteration it doesn't! – Saiteja Pabisetti Mar 31 '20 at 08:40
-
@phuclv _"atoi has undefined behavior"_: what makes you think that? Do you have any reference? – Jabberwocky Mar 31 '20 at 08:40
-
1@SaitejaPabisetti you're not supposed to post your entire code, just the relevant section (or more precisely, a section of code that demonstrates the problem). Just check out my link, which clearly shows that atoi() still works as expected – Felix G Mar 31 '20 at 08:40
-
@phuclv the thing I didn't understand is atoi uses strtol behind the scenes. – Saiteja Pabisetti Mar 31 '20 at 08:41
-
2@SaitejaPabisetti the fact that `atoi` uses `strtol` or not is irrelevant. `atoi` does what the [documentation](https://en.cppreference.com/w/c/string/byte/atoi) says, no more and no less. The bug is in _your code_. Maybe `atoi` is too limited for your usage, we don't know without seeing some source code. – Jabberwocky Mar 31 '20 at 08:43
-
@phuclv that's not exactly what you have written in your first document. – Jabberwocky Mar 31 '20 at 08:45
-
1@Jabberwocky that's exactly what I said: atoi can invoke UB – phuclv Mar 31 '20 at 08:50
-
2`atoi` has been used for decades. If *at a moment in your program* `atoi("20")` returns `0`, it is just an evidence that *anywhere in the program* you have Undefined Behaviour. C is a low level language, and consequences of UB are not necessary local. That is even the reason why it is called *Undefined Behaviour*... – Serge Ballesta Mar 31 '20 at 08:53
-
3I reckon the problem will be solved in an instant once you provide the relevant code. I really doubt that your program has `atoi("20")` with a literal string in it. It's more likely to be `stoi(str)`, where you think that `str` should be `"20"`, but don't check. Your comment that "in the first iteration it works and in the second iteration it doesn't" makes it clear: The problem is in your code. – M Oehm Mar 31 '20 at 09:03
3 Answers
The explication from man
page:
The atoi()
function converts the initial portion of the string pointed to by nptr to int. The behavior is the same as:
strtol(nptr, NULL, 10);
except that atoi() does not detect errors. The atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long.
For more infos, you can see the difference of two functions in this topic atoi - strtol

- 3,607
- 1
- 9
- 29
Depending on the data, strtol()
has greater functionality
int easy = atoi(data);
// no errors detected
char *err;
errno = 0;
long tmp = strtol(data, &err, 10);
if (errno) /* deal with errno: overflow? */;
if (tmp > INT_MAX) /* int overflow */;
if (tmp < INT_MIN) /* int "underflow" */;
if (*err) /* extra data */;
int comprehensive = tmp; // convert from long

- 106,608
- 13
- 126
- 198
In addition to this answer by pmg that explains how strto...
has more functionality and error checking, the ato...
functions are guaranteed by the standard to behave like this (C17 7.22.1.2):
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)
That being said, ato...
are not required to call strto...
internally (though this is the case in many libs), just to behave identically save for the error handling.
Please note that the ato...
functions are not formally listed as obsolete yet. But they should be avoided still, mainly because it is pointless to use atoi
when we can as well use strtol(str, NULL, 10)
, the latter being identical but with better error handling.

- 195,001
- 40
- 254
- 396