Strtol Function In C

The strtol function disregards any leading whitespace characters in the string, processes the subsequent characters as numerical values, and stops once it encounters a non-numeric character.

Syntax

The syntax of the strtol function in the C programming language is as depicted below:

Example

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

Arguments or Parameters used for this function:

A reference to a string that will be converted into a large integer.

endptr

The strtol function employs this parameter to indicate the position where the conversion stopped. If endptr is not a null pointer, strtol will update endptr to reference the initial character that was not converted.

It acts as the fundamental number that undergoes conversion. This base acts as the numerical system's base, which should range from 2 to 36. If the resulting number doesn't start with O (for Octal), Ox (for Hexadecimal), or OX (for Hexadecimal), it is assumed to be in decimal form when the base is zero.

Returns

The strtol function returns the integer value after converting a string. It disregards any leading white space characters, processes the subsequent numerical characters, and stops at the first non-numeric character encountered.

The errno variable gets assigned the value ERANGE when the strtol function tries to convert a number that is either excessively large or extremely small. If the number is too large to be converted, the function will output LONGMAX. Conversely, if the input number is too small to convert, the function will output LONGMIN.

Mandatory Header

The required header for the strtol function in the C programming language is:

Example

#include <stdlib.h>

Refers To

The strtol function is supported in the subsequent iterations of the C programming language:

Example

ANSI/ISO 9899-1990

Example of strtol

Let's consider an illustration demonstrating the utilization of the strtol function within a C program:

Example

/* example of utilising strtol by Logic Practice */

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

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

    /* Transfer a value into the variable */
    /* Whitespace is acceptable before the number */
strcpy(value, " 123");

    /* Transform the given value into a decimal long. */
    result = strtol(value, &eptr, 10);

    /* Check for errors if the outcome is 0. */
    if (result == 0)
    {
        /* Display a message and depart if there was a conversion error.*/
        if (errno == EINVAL)
        {
printf("Conversion error occurred: %d\n", errno);
exit(0);
        }
    }

    /* Examine the result for a range error if it equals LONG_MIN or LONG_MAX. */
    if (result == LONG_MIN || result == LONG_MAX)
    {
        /* Display a warning message if the value was out of range. */
        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;
}

Output:

The output of this program post compilation and running is:

Example

123 decimal
19e hexadecimal

Input Required

This code uses input(). Please provide values below: