![The C Standard Library Reference](/c_standard_library/images/c-mini-logo.jpg)
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - strtoul() function
The C stdlib library strtoul() function converts the initial part of the string to an unsigned long integer number according to the given base, which must be lies between 2 and 32 inclusive, or be the special value 0.
The strtoul() function is used when the size of the integer exceeds the range of normal integer types. It is also useful for converting strings representing numbers in different bases, such as binary to hexadecimal.
Syntax
Following is the C library syntax of the strtoul() function −
unsigned long int strtoul(const char *str, char **endptr, int base)
Parameters
This function accepts following parameters −
-
str − It is a string to be converted into a unsigned long integer value.
-
endptr − It is the reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
-
base − It represents the base of the number system(e.g., 10 for decimal, 16 for hexadecimal, or 0 for special case).
Return Value
This function returns the converted unsigned long int value. If the input string is not a valid, it returns 0.
Example 1
In this example, we create a basic c program to demonstrate how to use the strtoul() function.
#include <stdio.h> #include <stdlib.h> int main() { const char *str = "12532abc"; char *endptr; unsigned long int value; //use the strtoul() function value = strtoul(str, &endptr, 0); printf("The unsigned long integer is: %lu\n", value); printf("String part is: %s", endptr); return 0; }
Output
Following is the output −
The unsigned long integer is: 12532 String part is: abc
Example 2
The following example convert a string's number part to an unsigned long hexadecimal integer, using the strtoul() function.
#include <stdio.h> #include <stdlib.h> int main() { // initializing the string char str[150] = "202405Tutorialspoint India"; // reference pointer char* endptr; unsigned long int value; // finding the unsigned long // integer with base 16 value = strtoul(str, &endptr, 16); // displaying the unsigned number printf("The unsigned long integer is: %lu\n", value); printf("String in str is: %s", endptr); return 0; }
Output
Following is the output −
The unsigned long integer is: 2106373 String in str is: Tutorialspoint India
Example 3
Let's create another example, we try to convert the string that does not contains any numeric characters to the unsigned long decimal number.
#include <stdio.h> #include <stdlib.h> int main() { const char *str = "abcde"; char *endptr; unsigned long int value; //use the strtoul() funcion value = strtoul(str, &endptr, 10); if (value == 0 && endptr == str) { printf("No numeric characters were found in the string.\n"); } printf("unsigned long integer is: %lu\n", value); printf("String part is: %s", endptr); return 0; }
Output
Following is the output −
No numeric characters were found in the string. unsigned long integer is: 0 String part is: abcde