Atol () v / s. strtol ()

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

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

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

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

+53
c strtol
Sep 25 '10 at 5:48
source share
7 answers

strtol provides you with great flexibility, as it can really tell you whether the entire string has been converted to an integer or not. atol , when it is impossible to convert a string to a number (for example, to 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 indicate using the 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 . This is a little more difficult to use, but it has a good reason, as I explained above.

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

+76
Sep 25 '10 at 5:54
source share
Functionality

atol is a subset of strtol functionality, except that atol provides you with unsuitable error handling capabilities. The most noticeable problem with ato... functions is that they lead to undefined behavior in case of overflow. Note: this is not just the absence of informational feedback in case of an error, this is undefined behavior, that is, as a rule, an unrecoverable failure.

This means that the atol function (like all other ato.. functions) is practically useless for any serious practical purposes. It was a design error, and its place is in the dump of the C story. You must use functions from the strto... group to perform the transformations. They were introduced, among other things, to fix problems inherent in the functions of the ato... group.

+20
Sep 25 '10 at 5:57
source share

According to the atoi man page, it has deprecated 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. 
+18
Nov 08
source share

In the new code, I would always use strtol . It has error handling, and the endptr argument allows you to see how much of the string has been used.

The C99 standard describes the functions of ato* :

With the exception of error behavior, they are equivalent

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

+4
Sep 25 '10 at 5:55
source share

atol(str) equivalent

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

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

+4
Sep 25 '10 at 5:55
source share

If memory is used, strtol() has the added benefit of setting (optionally) endptr to indicate the first character that cannot be converted. If NULL , it is ignored. Thus, if you process a string containing numbers and characters mixed, you can continue.

eg.

 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" */ 
+2
Sep 25 '10 at 5:56
source share

The strtol man page 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. (The modified Eli code is 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; } 
+1
Dec 05
source share



All Articles