Someone told me that I shouldn't use atoi()
, and that I should always use strtol()
instead. What's wrong with atoi()
that I shouldn't use it? Is strtol()
really the right thing to use instead? (And what about the fact that strtol()
returns a long
, not an int
like atoi()
does?)

- 4,869
- 3
- 17
- 30
-
2Note: I know perfectly well why not to use `atoi()`. However, while I see questions about how to use `atoi()` that explain why not to use it, I haven't been able to find this direct question, and I feel it would be useful to have the question and answer linked together properly as a pair. – This isn't my real name Jul 17 '13 at 20:54
-
1(Also, I'm curious to see what answers people come up with. Variation can be interesting.) – This isn't my real name Jul 17 '13 at 20:55
-
1Hasn't this been discussed to death? Have you looked around this website a bit? – Kerrek SB Jul 17 '13 at 20:58
-
4Yes, it's been discussed to death. Yes, I've looked around the website. What I was looking for was a question that said "why shouldn't I use `atoi()`", and I didn't find one. What I found many of were questions about how to use `atoi()` that were basically answered by "Don't, and here's why", but I felt that the question of why _not_ to use `atoi()` warranted a separate question. As this question has now been marked a duplicate of the one I was looking for and didn't find, I have I what was looking for, and am satisfied. – This isn't my real name Jul 17 '13 at 21:16
-
https://blog.mozilla.org/nnethercote/2009/03/13/atol-considered-harmful/ – phuclv Mar 05 '17 at 01:00
-
3It's a good question. The other annoying thing about the "don't use `atoi`, use `strtol` instead` crowd is that it's significantly non-obvious how to use `strtol` correctly. So far none of the answers here explain that. I'll try to find at least a link to one of the other questions where that's answered. – Steve Summit Jun 24 '18 at 11:23
-
3It's a good question. `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 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:04
3 Answers
from your own link:
The atoi() function is subsumed by strtol() but is retained because it is used extensively in existing code. If the number is not known to be in range, strtol() should be used because atoi() is not required to perform any error checking.
Or
atoi
is obsolete

- 30,851
- 12
- 72
- 100
With the atoi
there is no way of finding out if the passed string really is a number, as there is no special error "return". It also only handles decimal values (base 10), so can't handle arbitrary bases like e.g. strtol
. Also it can't handle values larger than signed integer (32 bits on most platforms).

- 400,186
- 35
- 402
- 621
If string will be much large and can't be converted, it causes undefined behaviour as value of that string can be too large and that may not be in range. In such cases(where number is not known to be in range) strtol()
should be used.

- 3,103
- 4
- 28
- 35