Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - strtol()

C Library Function - strtol()

C Standard Library - <stdlib.h> C Standard Library -

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

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 the errno variable in the header file to check whether an overflow has occurred.

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 - <stdlib.h> C Standard Library -