C Reference function atol()

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

Usage of atol():

long int atol ( const char * str );


C string str interpreting its content as a integer. White-spaces are discarded until the first non-whitespace character is found.

Return value:

The function returns the converted integral number as an int value, if successful and returns it as a long int. If no valid conversion could be performed then an zero value is returned.
If the value is out of range then LONG_MAX or LONG_MIN is returned.

Source code example of atol():


	int main ()
		long int long_int;
		char buffer [256];

		printf ("Enter a long number: ");
		gets ( buffer );

		long_int = atol (buffer);
		printf ("The value entered is %d.\n",long_int);

		return 0;

Output of the atol example program above:

	Enter a long number: 256000
	The value entered is 256000.

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.