9

I have been using atoi for a year now and I have an issue in the recent days that the expression:

atoi("20") is giving a value of 0 as output.

When I Google'd the issue I found that it is deprecated and strtol should be used instead.

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?

14
  • 6
    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 minimal reproducible example. Also read this: How to Ask Commented Mar 31, 2020 at 8:34
  • 2
    Please provide a minimal reproducible example. The atoi() function might be deprecated, but it does still work just fine: example Commented Mar 31, 2020 at 8:35
  • 3
    @SaitejaPabisetti the fact that atoi uses strtol or not is irrelevant. atoi does what the documentation 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. Commented Mar 31, 2020 at 8:43
  • 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... Commented Mar 31, 2020 at 8:53
  • 3
    I 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. Commented Mar 31, 2020 at 9:03

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

Comments

4

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 

1 Comment

I think that you'll have to #include errno.h and limits.h to take advantage of this greater functionality.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.