atol() v/s. strtol()
Asked Answered
B

8

70

What is the difference between atol() & strtol()?

According to their man pages, they seem to have the same effect as well as matching arguments:

long atol(const char *nptr);

long int strtol(const char *nptr, char **endptr, int base);

In a generalized case, when I don't want to use the base argument (I just have decimal numbers), which function should I use?

Ballistics answered 25/9, 2010 at 5:48 Comment(0)
B
100

strtol provides you with more flexibility, as it can actually tell you if the whole string was converted to an integer or not. atol, when unable to convert the string to a number (like in atol("help")), returns 0, which is indistinguishable from atol("0"):

int main()
{
  int res_help = atol("help");
  int res_zero = atol("0");

  printf("Got from help: %d, from zero: %d\n", res_help, res_zero);
  return 0;
}

Outputs:

Got from help: 0, from zero: 0

strtol will specify, using its endptr argument, where the conversion failed.

int main()
{
  char* end;
  int res_help = strtol("help", &end, 10);

  if (!*end)
    printf("Converted successfully\n");
  else
    printf("Conversion error, non-convertible part: %s", end);

  return 0;
}

Outputs:

Conversion error, non-convertible part: help

Therefore, for any serious programming, I definitely recommend using strtol. It's a bit more tricky to use but this has a good reason, as I explained above.

atol may be suitable only for very simple and controlled cases.

Breastsummer answered 25/9, 2010 at 5:54 Comment(6)
I believe in your example, the condition should be if (!*end). It will be pointing to the null-terminator of the string (if it was all converted) but won't be set to NULL itself.Reremouse
Does this same logic apply to the linux kernel function simple_strtol? I cannot get the if (!*end) condition to evaluate to true, using your exact model.Sulfaguanidine
This does not handle error related to numbers out of range. For that errno needs to be checked!Microphysics
Thank you very much. I tried atol with string "123ABC" and it did give me value 123. so you are right "for any serious programming, I definitely recommend using strtol". :)Scute
Note that POSIX 1003.1 does not guarantee that 0 is returned by atol() in case of error. Quote: If the value cannot be represented, the behavior is undefined.Shelashelagh
This does not work when the string is empty (contains only the null terminator). Unless you are willing to accept that it gets converted to 0.Bushwhack
D
24

atol functionality is a subset of strtol functionality, except that atol provides you with no usable error handling capabilities. The most prominent problem with ato... functions is that they lead to undefined behavior in case of overflow. Note: this is not just a lack of informative feedback in case of an error, this is undefined behavior, i.e. generally an unrecoverable failure.

This means that atol function (as well as all other ato.. functions) is pretty much useless for any serious practical purposes. It was a design mistake and its place is on the junkyard of C history. You should use functions from strto... group to perform the conversions. They were introduced, among other things, to correct the problems inherent in functions of ato... group.

Debonair answered 25/9, 2010 at 5:57 Comment(0)
H
19

According to the atoi man page, it has been deprecated by strtol.

IMPLEMENTATION NOTES
The atoi() and atoi_l() functions have been deprecated by strtol() and strtol_l() 
and should not be used in new code.
Haft answered 8/11, 2012 at 5:34 Comment(0)
A
6

In new code I would always use strtol. It has error handling and the endptr argument allows you to see which part of the string was used.

The C99 standard states about the ato* functions:

Except for the behavior on error,they equivalent to

atoi: (int)strtol(nptr,(char **)NULL, 10)
atol: strtol(nptr,(char **)NULL, 10)
atoll: strtoll(nptr, (char **)NULL, 10)

Acquah answered 25/9, 2010 at 5:55 Comment(0)
P
5

atol(str) is equivalent to

strtol(str, (char **)NULL, 10);

Use strtol if you want the end pointer (to check whether there are more characters to read or if in fact you have read any at all) or a base other than 10. Otherwise, atol is fine.

Pelops answered 25/9, 2010 at 5:55 Comment(0)
R
2

If memory serves, strtol() has the added benefit to set the (optional) endptr to point to the first character that could not be converted. If NULL, it is ignored. That way if you're processing a string containing numbers and characters mixed, you could continue.

e.g.,

char buf[] = "213982 and the rest";
char *theRest;
long int num = strtol(buf, &theRest, 10);
printf("%ld\n", num);    /* 213982 */
printf("%s\n", theRest); /* " and the rest" */
Reremouse answered 25/9, 2010 at 5:56 Comment(0)
M
1

The man page of strtol gives the following:

ERRORS
   EINVAL (not in C99) The given base contains an unsupported value.
   ERANGE The resulting value was out of range.
   The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).

The following code checks for range errors. (Modified Eli's code a bit)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
   errno = 0;
   char* end = 0;
   long res = strtol("83459299999999999K997", &end, 10);

   if(errno != 0)
   {
      printf("Conversion error, %s\n", strerror(errno));
   }
   else if (*end)
   {
      printf("Converted partially: %i, non-convertible part: %s\n", res, end);
   }
   else
   {
      printf("Converted successfully: %i\n", res);
   }

   return 0;
}
Microphysics answered 5/12, 2012 at 17:41 Comment(0)
H
0

Though the question is old, the content is not as it is about an integral part of the language.

Other answers fail to emphasize two important detail: Both atol and strl will do the same thing but atol tries to parse only a base 10 format (signs and digits) and will give no error.

  • While strtol (and all strto_ that return integer type) can fetch a number in any base ranging from 2 to 36, all ato_ functions (atoi, atol) will fetch only base 10.

  • Both will return zero (0) if no parse is possible, yet strtol can give the location where parse is ended and an error if the number was too big/small.

side note: any strto_ or ato_ that returns a decimal number uses base 10, meaning digits and a few other characters to represent a floating-point number.

Helvellyn answered 14/6, 2022 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.