6.19.11 atol Function
Converts a string to a long integer.
Include
<stdlib.h>
Prototype
long atol(const char *s);
Argument
s
- string to be converted
Return Value
Returns the converted long integer if successful; otherwise, returns 0.
Remarks
The number may consist of the following:
[whitespace] [sign] digits
Optional whitespace followed by an optional sign, then a sequence of one or
more digits. The conversion stops when the first unrecognized character is reached. The
conversion is equivalent to (int) strtol(s,0,10)
, except it does no
error checking so errno
will not be set.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a[] = " -123456";
char b[] = "2Number";
long x;
x = atol(a);
printf("String = \"%s\" int = %ld\n", a, x);
x = atol(b);
printf("String = \"%s\" int = %ld\n", b, x);
}
Example Output
String = " -123456" int = -123456
String = "2Number" int = 2