C Library Functions -strtol()

C Standard Library - <stdlib.h>

Description

C Library Functionslong int strtol(const char *str, char **endptr, int base)Pass the parameterstrThe string pointed to according to the givenbaseConverts to a long integer (of type long int). The base must be between 2 and 36 (inclusive), or the special value 0.

Declaration

Below is the declaration of the strtol() function.

long int strtol(const char *str, char **endptr, int base)

Parameter

  • str-- The string to be converted to a long integer.
  • endptr-- A reference to an object of type char*, whose value is set by the function tostrthe next character after the numeric value.
  • base-- The base, which must be between 2 and 36 (inclusive), or the special value 0. If base is 0, the radix is determined by the prefix of the string: if the string starts with '0x' or '0X', it is treated as hexadecimal; if the string 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 the numeric format, strtol() will return 0. If the conversion result exceeds the representable range of a long integer, overflow will occur, and errno will be set to ERANGE. You can use the errno variable from the <errno.h> header file to check whether overflow has occurred.

Example

The following example demonstrates the usage of the strtol() function.

In the following example, we convert the string "12345" into a long integer. The strtol() function will convert this string into the corresponding integer value 12345. Because we specified base as 10, it will perform the conversion in decimal.

Example

#include <stdio.h>
#include <stdlib.h>

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 us compile and run the above program, which will produce the following result:

转换结果:12345

If the input string cannot be completely converted to an integer, the strtol() function returns the successfully converted portion, 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 starting position of "ab", indicating that the conversion failed.

Example

#include <stdio.h>
#include <stdlib.h>

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" into a long integer. The strtol() function will attempt to convert this string into 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>

other extensions