Javatpoint Logo
Javatpoint Logo

Strtoul() Function in C

Introduction:

The C/C++ strtoul() function transforms the first character of the string in str into an unsigned long int value with the specified base, which must be between 2 and 36 inclusively or have the special value 0. Once the first non-whitespace character is detected, this function throws away any white space characters. After that, it takes as many characters as necessary to make a valid base-n unsigned integer number representation and transforms them to an integer value.

Syntax

The C language's syntax for the strtoul function is as follows:

Arguments or Parameters used:

nptr

It represents an unsigned long integer that can be created from a pointer to a string.

endptr

The strtoul function uses it to specify where the conversion stopped. If endptr is not a null pointer, the strtoul function will change endptr such that it now points to the first character that was not converted.

base

It is the number's base, which is being transformed. The base serves as the number's radix, which must fall between 2 and 36. Unless the converted number begins with O (for Octal), Ox (for Hex), or OX (for Hex), the number is believed to be decimal if the base is zero.

Returns

The unsigned long integer representation of a string is what the strtoul function returns. The strtoul function skips over all white space at the start of the string, turns the characters after that as numbers, and then terminates when it reaches the first character that is not a number.

The errono variable will be set to ERANGE if the strtoul function attempts to convert a value that is either too large or too small.

Example of strtoul

Let's take a look at an example of how the strtoul function might be used in a C program:

Output:

321 decimal
6e8 hexadecimal

Another example of strtoul in C:

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA