Strtoul Function In C

Syntax

The syntax for the strtoul function in the C programming language is outlined below:

Example

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

Arguments or Parameters used:

It denotes an unsigned long integer that can be generated from a string pointer.

endptr

The strtoul function utilizes the endptr parameter to indicate the point where the conversion process halted. If endptr is not a null pointer, strtoul will update endptr to point to the first character that was not successfully converted.

It represents the numerical base that is undergoing conversion. This base acts as the numeric system's radix, which should be within the range of 2 to 36. In cases where the resulting number starts with 0 (for Octal), 0x (for Hexadecimal), or 0X (for Hexadecimal), the number is assumed to be in decimal format if the base is zero.

Returns

The value returned by the strtoul function represents an unsigned long integer derived from a string. This function disregards any initial whitespace in the string, interprets subsequent characters as numerical values, and stops processing once a non-numeric character is encountered.

The errono variable gets assigned the value ERANGE when the strtoul function tries to convert a value that is either too big or too small.

Example of strtoul

Let's consider an illustration showcasing how the strtoul function could be employed within a C program:

Example

/* Example using strtoul by Logic Practice */

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

int main(int argc, const char * argv[])
{
    /* Set up random variables. */
    char value[10];
    char *eptr;
    unsigned long result;

    /* Adding a value to the variable */
    /* Whitespace before the number is acceptable. */
strcpy(value, " 321");

    /* Change the value provided to a decimal unsigned long. */
    result = strtoul(value, &eptr, 10);

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

        /* Display a warning message if the value entered was outside of acceptable range.*/
        if (errno == ERANGE)
printf("The value provided was out of range\n");
    }

    /* Show the conversion result */
printf("%lu decimal\n", result);

    /* Insert a hex value into the variable. */
strcpy(value, "0x6e8");

    /* Transform the supplied value into an unsigned hexadecimal long. */
    result = strtoul(value, &eptr, 16);

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

        /* Display a warning message if the value entered was outside of acceptable range. */
        if (errno == ERANGE)
printf("The value provided was out of range\n");
    }

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

    return 0;
}

Output:

Output

321 decimal
6e8 hexadecimal

Another example of strtoul in C:

Example

/**
 * String to unsigned long int conversion in another C programme using strtoul() library function.
 */

#include <stdio.h>
#include <stdlib.h>     // Used for strtoul()


int main()
{
    char number[30];
    char* endPtr;

    unsigned long int bigNumber;
    int base;


    /* String representation of the user-inputted integer. */
printf("Enter any number: ");
    gets(number);


printf("Enter base: ");
scanf("%d", &base);


    /* Convert a number's string representation to an unsigned long */
bigNumber = strtoul(number, &endPtr, base);


    /* For unsuccessful conversions, endPtr points to NULL. */
    if(*endPtr)
printf("Unable to convert '%s' to base %d.", number, base);
    else
printf("Converted unsigned long int = %lu\n", bigNumber);


    return 0;
}

Output:

Output

Enter any number: 1898
Enter base: 10
Converted unsigned long int = 1898

Enter any number: 10110
Enter base: 2
Converted unsigned long int = 22

Enter any number: ab
Enter base: 16
Converted unsigned long int = 171

Input Required

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