strtol function(Convert String to Long Integer) in C Language – example and detail

In the C Programming Language, the strtol characteristic converts a string to a long integer.

The strtol characteristic skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that is not a number.

Syntax

The syntax for the strtol feature in the C Language is:

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

Parameters or Arguments

nptr A pointer to a string to convert to a long integer. endptr It is used by way of the strtol characteristic to point out where the conversion stopped. The strtol characteristic will modify endptr (if endptr is not a null pointer) so that endptr points to the first personality that used to be not converted. base The base of the quantity being converted. If base is between two and 36, it is used as the radix of the number. If base is zero, the number is assumed to be decimal except the transformed wide variety starts with O (for Octal), Ox (for hex) or OX (for hex).

Returns

The strtol feature returns the long integer representation of a string. The strtol characteristic skips all white-space characters at the starting of the string, converts the subsequent characters as phase of the number, and then stops when it encounters the first character that isn’t a number.

If the strtol function converts a cost that is too giant or too small to convert, it will store ERANGE in errno. If the cost is too massive to convert, the feature will return a cost of LONG_MAX. If the value is too small to convert the characteristic will return a value of LONG_MIN.

Required Header

In the C Language, the required header for the strtol feature is:

#include <stdlib.h>

Applies To

In the C Language, the strtol characteristic can be used in the following versions:

ANSI/ISO 9899-1990

strtol Example

Let’s seem at an instance to see how you would use the strtol characteristic in a C program:

/* Example using strtol by TechOnTheNet.com */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    char value[10];
    char *eptr;
    long result;

    /* Copy a value into the variable */
    /* It's okay to have whitespace before the number */
    strcpy(value, " 123");

    /* Convert the provided value to a decimal long */
    result = strtol(value, &eptr, 10);

    /* If the result is 0, test for an error */
    if (result == 0)
    {
        /* If a conversion error occurred, display a message and exit */
        if (errno == EINVAL)
        {
            printf("Conversion error occurred: %d\n", errno);
            exit(0);
        }
    }

    /* If the result is equal to LONG_MIN or LONG_MAX, test for a range error */
    if (result == LONG_MIN || result == LONG_MAX)
    {
        /* If the value provided was out of range, display a warning message */
        if (errno == ERANGE)
            printf("The value provided was out of range\n");
    }

    /* Display the converted result */
    printf("%ld decimal\n", result);

    /* Copy a hexadecimal value into the variable */
    strcpy(value, "0x19e");

    /* Convert the provided value to a decimal long */
    result = strtol(value, &eptr, 16);

    /* If the result is 0, test for an error */
    if (result == 0)
    {
        /* If a conversion error occurred, display a message and exit */
        if (errno == EINVAL)
        {
            printf("Conversion error occurred: %d\n", errno);
            exit(0);
        }
    }

    /* If the result is equal to LONG_MIN or LONG_MAX, test for a range error */
    if (result == LONG_MIN || result == LONG_MAX)
    {
        /* If the value provided was out of range, display a warning message */
        if (errno == ERANGE)
            printf("The value provided was out of range\n");
    }

    /* Display the converted result */
    printf("%lx hexadecimal\n", result);

    return 0;
}

When compiled and run, this application will output:

123 decimal
19e hexadecimal

Similar Functions

Other C features that are comparable to the strtol function:

atoi function atol feature strtod function strtoll feature strtoul feature

See Also

Other C functions that are noteworthy when dealing with the strtol function:

atof function <stdlib.h>