C strtol() Function | Convert String to Long Integer with Base Support

Introduction

The strtol() function in C is a standard library function that converts a string to a long integer. It is part of the C standard library (stdlib.h). This function is more versatile and robust than atol() because it allows for error checking and supports different number bases.

strtol() Function Syntax

The syntax for the strtol() function is as follows:

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

Parameters:

  • str: A C string that contains the representation of a long integer.
  • endptr: A pointer to a character pointer. If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr.
  • base: The base of the number represented by str. This can be any value from 2 to 36, or 0 to automatically detect the base.

Returns:

  • The function returns the converted long integer value. If no valid conversion could be performed, it returns 0.

Examples

Converting a Simple String to Long Integer

To demonstrate how to use strtol() to convert a string to a long integer, we will write a simple program.

Example

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

int main() {
    const char *str = "1234567890";
    char *endptr;
    long int num;

    // Convert string to long integer
    num = strtol(str, &endptr, 10);

    // Print the converted value
    printf("The converted value is: %ld\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 1234567890

Handling Invalid Input

This example shows how strtol() behaves with invalid input.

Example

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

int main() {
    const char *str = "abc123456";
    char *endptr;
    long int num;

    // Convert string to long integer
    num = strtol(str, &endptr, 10);

    // Print the converted value
    printf("The converted value is: %ld\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 0
Remaining part of the string: abc123456

Converting Strings with Different Bases

This example shows how strtol() can be used to convert strings with different number bases.

Example

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

int main() {
    const char *str = "1A3F";
    char *endptr;
    long int num;

    // Convert hexadecimal string to long integer
    num = strtol(str, &endptr, 16);

    // Print the converted value
    printf("The converted value is: %ld\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 6719

Real-World Use Case

Converting User Input to Long Integer with Error Checking

In real-world applications, the strtol() function can be used to convert user input, provided as a string, into a long integer with proper error checking.

Example

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

int main() {
    char input[100];
    char *endptr;
    long int value;

    // Prompt the user for input
    printf("Enter a long integer: ");
    fgets(input, sizeof(input), stdin);

    // Convert input to long integer
    value = strtol(input, &endptr, 10);

    // Check for errors
    if (endptr == input) {
        printf("No valid conversion could be performed.\n");
    } else {
        // Print the converted value
        printf("You entered: %ld\n", value);

        // Print the remaining part of the string
        if (*endptr != '\0' && *endptr != '\n') {
            printf("Remaining part of the string: %s\n", endptr);
        }
    }

    return 0;
}

Output (example user input "9876543210abc"):

Enter a long integer: 9876543210abc
You entered: 9876543210
Remaining part of the string: abc

Conclusion

The strtol() function is useful for converting strings that represent long integers into long integer values. It provides error handling and supports different number bases, making it more flexible and reliable for numerical data conversion.

Comments