strtol
Converts a string to a long value.
Defined in header <stdlib.h>
Syntax
#include <stdlib.h>
long strtoUconst char *s, char **endptr, intradix);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ |
Remarks
strtol converts a character string, s, to a long integer value, s is a sequence of characters that can be interpreted as a long value; the characters must match this generic format:
| [ws] [sn] | [X] [ddd] |
where
[ws] = optional whitespace [sn] = optional sign (+ or -) [0] = optional zero (0) [x] = optional x or X [ddd] = optional digits
strtol stops reading the string at the first character it doesn't recognize.
If radix is between 2 and 36, the long integer is expressed in base radix. If radix is 0, the first few characters of s determine the base of the value being converted.
| First | Second | |
| character | character | String interpreted as |
| 0 | 1-7 | Octal |
| 0 | xor X | Hexadecimal |
| 1-9 | Decimal |
If radix is 1, it is considered to be an invalid value. If radix is less than 0 or greater than 36, it is considered to be an invalid value.
Any invalid value for radix causes the result to be 0 and sets the next character pointer *endiptr to the starting string pointer.
If the value in s is meant to be interpreted as octal, any character other than 0 to 7 will be unrecognized.
If the value in s is meant to be interpreted as decimal, any character other than 0 to 9 will be unrecognized.
If the value in s is meant to be interpreted as a number in any other base, then only the numerals and letters used to represent numbers in that base will be recognized. (For example, if radix equals 5, only 0 to 4 will be recognized; if radix equals 20, only 0 to 9 and A to / will be recognized.)
If endptr is not null, strtol sets ^endptr to point to the character that stopped the scan {^endptr = &iStopper).
Return value
strtol returns the value of the converted string, or 0 on error.
See also
Example
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string = "87654321", *endptr;
long 1number;
/* strtol converts string to long integer */
Inumber = strtol(string, &endptr, 10);
printf("string = %s long = %ld\n", string, Inumber);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/stdlib.h/strtol/
· short form /3.1/strtol/