atoi in c

If you want to know more on atoi in c then read this blog post.

What is atoi in c programming?

atoi is a function which converts the given string to an integer.

This function returns the converted integral number. If conversion is not possible then it returns zero.

This function converts string to an integer until it encounters character which cannot be converted to an integer.

This function skips all the whitespace characters at the beginning of the string.

It is defined in stdlib.h header file.

Prototype of atoi

#include<stdlib.h>
int atoi(const char *string);

Parameters of atoi in c

atoi takes string as an argument.

Return value of atoi in c

atoi returns integer value. If conversion is not possible then it returns zero.


Example program for atoi in c

Example 1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
  char str[30]="1234567";
  int result;
  result=atoi(str);
  printf(" The integer value is %d \n",result);
  return 0;
}
example 1 for atoi in c
example 1 for atoi in c

Code explanation

The atoi function is called by passing the input string. The input string is converted to an integer value and the result is being printed onto the console.


Example 2

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
  int result;
  char str[30]="scholarsoul";
  result=atoi(str);
  printf("The result is %d \n\n",result);
  return 0;
}
example 2 for atoi in c
example 2 for atoi in c

Code explanation

The atoi function is called by passing the input string. The input string is converted to an integer value and the result is being printed onto the console.


Difference between atoi and itoa in c

atoi in c is a function that converts the given string to an integer. itoa function in c converts an integer to string using a given base.

atoi function returns the converted integral number. If the conversion is not possible then it returns zero. itoi function returns the pointer to the resulting string.

atoi function takes one argument. itoi function takes three arguments.

atoi function terminates the process when it encounters a character which cannot be converted to an integer. itoa function terminates the process when it encounters a null value.

atoi in citoa in c
Converts the given string to an integer.Converts integer to string using a given base.
int atoi(const char *string);char *itoa(int value, char *buffer,int radix);
Takes one argument.Takes three arguments.
Difference between atoi and itoa in c programming

Write your own atoi()

We have already seen the built in atoi function from stdlib.h.

Now lets write our own function which converts string (character array) to a number and returns the number.

Before writing the function lets see the parameters and return value for our function.

Return value should be the integer number. Parameter is the string.

Now let’s see the basic idea of converting the string to int. We check the first character if the first character is ‘-‘ then mark it as a negative number (use flags). Otherwise, follow the below steps.

We have to declare a temporary integer number. Now start the loop from end on the character array.

For each character from the end check, it has the ASCI value of numbers if it has then we have to convert the character to the digit. Then assign its position such as ones, tens, hundreds…
Then return the number. Otherwise, return -1.

Lets have a look at the program

#include<stdio.h>
#include<string.h>
int our_atoi(char *number_string)
{
	int rv=0;
	int negative=0;
	if( strlen(number_string)==0|| strlen(number_string)<0)return 0;
	if(number_string[0]=='-')negative=1;
	int n,zero_one;
	if(negative)
		zero_one=1;
	else
		zero_one=0;
	int i=zero_one;
	while(number_string[i]>='0' && number_string[i]<= '9')
	{
		if( !(number_string[i]>='0' && number_string[i]<= '9') )return -1;
		n=number_string[i]-'0';
		rv=(rv*10)+n;
		i++;
	}

 if (negative) return (-1*rv);
return rv;
}
int main(int argc, char const *argv[])
{
	char number[10];
	printf("\n\n\tYour own atoi\n\tEnter a number\n\t");
	scanf("%s",number);
	printf("\n\tConverting a string using our own atoi.\n\t");
	printf("\n\tResult is: %d\n\n\t",our_atoi(number));
    
	return 0;
}


People also ask for

What is atoi in c?

atoi in c programming is a function which converts the given string to an integer.

How does atoi works in c?

In c programming atoi converts the given string to an integer. It converts the string to an integer character by character and stops when it encounters a character that cannot be converted to an integer.

What does atoi stands for?

atoi stands for ASCII to integer. It is defined in stdlib.h header file.

What error does atoi returns?

atoi returns an integer value of the converted string. If the character is not convertible it returns zero. It does not return any error values.


People also read