C Reference function strtoul()

This function of stdlib will convert a string to an unsigned long integer.

Usage of strtoul():

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


C string str interpreting its content as an unsigned integral number of the specified base, which is returned as an unsigned long int value.

Return value:

The function returns the converted floating point number as a unsigned long int value, if successful.
If no valid conversion could be performed then an zero value is returned.
If the value is out of range then ULONG_MAX is returned.

Source code example of strtoul():


	int main ()
		char buffer[256];
		unsigned long ul;

		printf ("Enter an unsigned number: ");
		fgets (buffer,256,stdin);

		ul = strtoul (buffer,NULL,0);
		printf ("Output: %lu\n",ul);
		return 0;

Output of the strtoul example program above:

	Enter an unsigned number: 12
	Output: 12

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.