Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atol() v/s. strtol()

Tags:

c

strtol

People also ask

Which of the following is a difference between strtol () and Atol ()?

The strtoll and strtoull subroutines provide the same functions but return long long integers. The atol subroutine is equivalent to the strtol subroutine where the value of the EndPointer parameter is a null pointer and the Base parameter is a value of 10.

What is strtol () in C?

The strtol library function in C converts a string to a long integer. The function works by ignoring any whitespace at the beginning of the string, converting the next characters into a long integer, and stopping when it comes across the first non-integer character.

What is the difference between atoi and Atol?

1 Answer. Show activity on this post. the only difference is that atoi casts the return value to int . There could be different behavior in error cases (when the string represents a value that is out of range of the type), since behavior is undefined for both.

What does strtol return if fails?

RETURN VALUE top The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX. In both cases, errno is set to ERANGE.


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.


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.


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.

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.


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)