Header File
stdlib.h
Category
Conversion Routines, Math Routines
Prototype
long strtol(const char *s, char **endptr, int radix);
long wcstol(const wchar_t *s, wchar_t **endptr, int radix);
Description
Converts a string to a long value.
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] [0] [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.
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 *endptr 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 J will be recognized.)
If endptr is not null, strtol sets *endptr to point to the character that stopped the scan (*endptr = &stopper).
Return Value
strtol returns the value of the converted string, or 0 on error.
Example
#include <stdlib.h> #include <stdio.h> int main(void) { char *string = "87654321", *endptr; long lnumber; /* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10); printf("string = %s long = %ld\n", string, lnumber); return 0; }
Portability
|
POSIX |
Win32 |
ANSI C |
ANSI C++ |
strtol |
+ |
+ |
+ |
+ |
wcstol |
|
+ |
+ |
+ |
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|