C Library Function - strtoul()
C Standard Library - <stdlib.h>
Description
The C library function unsigned long int strtoul(const char str, char *endptr, int base) converts the initial part of the string pointed to by str to an unsigned long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
Declaration
Here is the declaration for the strtoul() function.
unsigned long int strtoul(const char *str, char **endptr, int base)
Parameters
str -- The string to be converted to an unsigned long integer.
endptr -- A reference to a char* object, whose value is set by the function to the next character in str after the numerical value.
base -- The base, which must be between 2 and 36 inclusive, or be the special value 0.
Return Value
The function returns the converted long integer, or zero if no conversion could be performed.
Example
The following example demonstrates the use of the strtoul() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[30] = "2030300 This is test";
char *ptr;
long ret;
ret = strtoul(str, &ptr, 10);
printf("The number (unsigned long integer) is %lu\n", ret);
printf("The string part is |%s|", ptr);
return(0);
}
Let's compile and run the above program, this will produce the following result:
The number (unsigned long integer) is 2030300
The string part is | This is test|