C Reference function atoi() convert a string to an integer

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

Usage of atoi():

int atoi ( const char * str );

Parameters:

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. If no valid conversion could be performed then an zero value is returned. If the value is out of range then INT_MAX or INT_MIN is returned.

Source code example of atoi():


	#include<stdio.h>
	#include<stdlib.h>

	int main ()
	{
		int i;
		char buffer [256];

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

		i = atoi (buffer);
		printf ("The value entered is %d.",i);

		return 0;
	}

Output of the atoi example program above:


	Enter a number:12

	The value entered is 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. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

Leave a Reply: