C Library Function - strtol()
Description
C Library Function **long int strtol(const char *str, char endptr, int base) Converts the string pointed to by the argument str converts the string pointed to by base into a long integer (of type long int) according to the given
Declaration
Below is the declaration of the strtol() function.
long int strtol(const char *str, char **endptr, int base)
Parameters
- str -- String to be converted to a long integer.
- endptr -- A reference to an object of type char*, whose value is set by the function to str the next character after the numeric value in
- base -- The base, which must be between 2 and 36 (inclusive), or the special value 0. If base is 0, the base is determined from the string's prefix: if the string starts with '0x' or '0X', it is treated as hexadecimal; if it starts with '0', it is treated as octal; otherwise it is treated as decimal.
Return Value
The function returns the converted long integer value. If the input string does not conform to a numeric format, strtol() returns 0. If the conversion result is out of the range representable by a long integer, an overflow occurs and errno is set to ERANGE. You can use the
Example
Below is the declaration of the strtol() function.
In the following example, we convert the string "12345" to a long integer. The strtol() function converts this string to the corresponding integer value 12345. Because we specified base as 10, it performs the conversion in decimal.
Example
#include
#include
int
main
(
)
{
char
str
[
]
=
"12345"
;
char
endptr
;
long
int
num
;
num
=
strtol
(
str
,
&
endptr
,
10
)
;
if
(
endptr
!=
'\0'
)
{
printf
(
"Conversion failed: the input string is not a valid integer.\n"
)
;
}
else
{
printf
(
"Conversion result: %ld\n"
,
num
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
转换结果:12345
If the input string cannot be fully converted to an integer, the strtol() function returns the part that was successfully converted, and endptr points to the first character of the unconverted part. In this example, endptr points to the null character '\0' at the end of the string, indicating that the entire input string was successfully converted to an integer.
If the input string contains non-numeric characters, such as "12ab", then endptr will point to the start of "ab", indicating a conversion failure.
Example
#include
#include
int
main
(
)
{
char
str
[
]
=
"12ab"
;
char
endptr
;
long
int
num
;
num
=
strtol
(
str
,
&
endptr
,
10
)
;
if
(
endptr
!=
'\0'
)
{
printf
(
"Conversion failed: the input string is not a valid integer. Unconverted part: %s\n"
,
endptr
)
;
}
else
{
printf
(
"Conversion result: %ld\n"
,
num
)
;
}
return
0
;
}
In the above example, we convert the string "12ab" to a long integer. The strtol() function attempts to convert this string to the corresponding integer value 12, but because the string contains the non-numeric characters "ab", the conversion cannot be completed.
The output will be:
转换失败:输入字符串不是一个有效的整数。未转换部分:ab
C Standard Library -