Why shouldn't I use atoi ()?

Someone told me that I should not use atoi() , and that I should always use strtol() instead. What happened to atoi() that I should not use it? Is strtol() correct to use? (What about the fact that strtol() returns a long and not int like atoi() does?)

+6
source share
3 answers

from your link :

The atoi () function is added by strtol (), but saved because it is widely used in existing code. If the number is unknown in the range, strtol () must be used because atoi () is not required to check for errors.

or

atoi deprecated

+17
source

With atoi there is no way to find out if the passed string is really a number since there is no special "return" error. It also processes only decimal values ​​(base 10), so it cannot process arbitrary bases, for example, for example. strtol . Also, it cannot handle values ​​larger than the signed integer (32 bits on most platforms).

+4
source

If the string is very large and cannot be converted, it causes undefined behavior, since the value of this string may be too large and may not be in the range. In such cases (where the number is unknown in the range ) strtol() .

+4
source

Source: https://habr.com/ru/post/949681/


All Articles