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. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 15 responses to “C Reference function atoi() convert a string to an integer”

Why not let us know what you think by adding your own comment!

  1. sarah on March 17th, 2012:

    best answer i got here……..thankyou!!!

  2. sarah on March 17th, 2012:

    i didnt understand the fgets arguments..

  3. sarah on March 17th, 2012:

    i tried using atoi but geting an error saying TYPE MISMATCH IN PARAMETER ‘_S’ IN CALL TO ATOL….can anybody help me out?

  4. rebecca on October 13th, 2012:

    I mean for example if the user will input the word “eleven” the output must be 11?

  5. Sagar on January 10th, 2013:

    @rebecca: NO! It’s just that, if a number is stored in form of string, atoi() will convert it into an integer, so now you can perform integral operations on it.
    eg: char *str = “11”; //you cannot perfrom mathematical operations on str
    int x = atoi(str); //now x will contain numerical value 11

  6. Lenny on June 25th, 2013:

    What happens if you need to include 0 in your input range?

  7. abhi on August 26th, 2013:

    what if the user inputs “A”…..will atoi() return the ascii value of A ??

  8. Arka on September 9th, 2013:

    @abhi

    It will return 0 not the ASCII value of the character.

  9. venkatesh on September 14th, 2013:

    if we want to convert string into integer For Example we have str[]= “a1b2c3” we want 1 as an integer so just subtract the ASCII value of zero from the ASCII value of 1.
    code
    int a=str[1]-‘0’;

  10. bhaghath pj on November 11th, 2013:

    how to get 12 from string a12b?

  11. sdrath on November 15th, 2013:

    @bhaghath pj, that’s what atoi does.

    What if I have multiple numbers stored in the array, let’s say s[]=”abc1def23ghi90″ and I have to store them seperately for further usage?
    Would this be possible with atoi?

  12. Rangappa Patel on December 20th, 2013:

    I want implementation code for function atoi()

  13. Sam on January 5th, 2014:

    @bhaghath pj… Easy way to write code like this…(just only get for 12 from a12b.. You can change this different ways).
    int i;
    char x[]=”a12b”;
    i=atoi(&x[1]);
    printf(“%d\n”,i);

  14. Bhupal Reddy on January 24th, 2014:

    Hi all,
    i need to convert asci to integer without using atoi function…(inputs “12a” or “bgdd”…

  15. Mangesh on February 3rd, 2014:

    What if I have multiple numbers stored in the array, let’s say s[]=”1def3ghi0″ and I have to store them seperately for further usage?
    Would this be possible with atoi???????????????????????