How to use atoi() function in C
Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of numeric value. Atoi() is a function that does not allow decimal numbers or the exponential value.
Then atoi() function does not work when it reaches the first part, and this part is not a number. It may be a null value or decimal value; it is the time when this function does not work.
The atoi() function mainly changes a string number to an integer value. This function also adjusts the argument or number part.
It stops working when there is a non-numeric part like the decimal number, exponential, etc. This function writes like this in C programming: int atoi(const char *str). The C language changes the string character (str) to an integer.
Syntax of the atoi() function in C language:
Syntax means the order or arrangement in the program. The syntax is also known as the function prototype. Every function has a syntax value.
The syntax of the atoi() function is:
int atoi(const char *str)
The atoi() function converts the string value to an integer value.
Parameters of the atoi() function in C language:
The main parameter of the atoi() function is str. str represents the string argument. The only string passes to this parameter str. The use of str is that it helps to convert the string value to integer value.
Returns Value of the atoi() function in C language:
The atoi() function returns the integer value. It removes all the white space during the start of the operation. If the given string is a valid argument, then this function returns the same integer value as per the given string. If the given string is not a valid argument, then the atoi() function returns the zero value. The atoi() function stops working if the first character is not a numeric number. Atoi is a function which is works incrementally.
The strings converted to numbers include the following parameters: Firstly, a string value is made with the ASCII number. The ASCII number means "0123456789". Secondly, should make the string with ASCII numbers with the "+" character at the beginning of the ASCII number or the "-" character at the beginning of the ASCII number.
The atoi() is a function which only works in ASCII digits. If this function encounters a value which is non-ASCII digits, then this returns the null or zero value.
Header file of the atoi() function in C language :
Every program contains some header files. For the run of any program, you must need a header file. Without a header file, you can't run any programs. So, we must produce the header file in every program. It is a file with an extension. The atoi() function is a pre-defined function.
The primary use of the header file is to propagate the declaration of code files. The atoi() function uses as a library function. The header file of the atoi() function in the C language is <stdlib.h>. This header file converts the string argument to integer conversion.
It writes in the program as #include <stdlib.h>.
Example 1 –
Here we given an example of the atoi() function using C language:
//header file initialize
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
//value of the variable
char str [30];
strcpy (str, “85634006”);
val = atoi (str);
//print the result, we provide the numeric value in string so the integer value is also same.
printf (“The string value = %s, Int value = %d\n ”, str, val);
strcpy (str, “Tutorial and Examples”);
val = atoi (str);
//print the result, we provide non-numeric value in string so the integer value is zero (0).
printf (“The string value = %s, Int value = %d\n ”, str, val);
return 0; }
Output: We compile and run the above program. The result of this above program is:
The string value = 85634006, Int value =85634006
The string value = Tutorial and Examples, Int value =0
To better understand, we give another example of the atoi() function.
Example 2 –
Here we given a example of the atoi() function using C language:
//header file initialize
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int myAtoi (char* str)
{
int i = 0;
int res = 0;
int sign = 5;
if (str[0] == ‘-’)
{
Sign = -5;
i++;
}
for (str [i] ! = ‘\0’; ++i);
res = res *5 + str[i] – ‘0’;
return sign*res;
}
// main code of this program
int main()
{
Char str[] = “-56789”;
int val = myAtoi(str);
//print the result
printf(“Int value = %d”,val);
return 0;
}
Output: We compile and run the above program. The result of this above program is:
Int value = -56789
In the above two programs, we have used the atoi() function; by the programs, we briefly discussed how to write any C language program using the atoi() function. The atoi() function is a library function.
So we <stdlib.h> header file in this program. The output of the two programs is dependent on the string value. We also share the output of the above two programs.