In C, atoi stands for ASCII To Integer. The atoi() is a library function in C that converts the numbers in string form to their integer value. To put it simply, the atoi() function accepts a string (which represents an integer) as a parameter and yields an integer value in return.
C atoi() function is defined inside <stdlib.h> header file.

Syntax of atoi() Function
int atoi(const char *strg);
Parameters
- strg: The string that is to be converted to an integer.
Note: The atoi function takes in a string as a constant (Unchangeable) and gives back the converted integer without modifying the original string.
Return Value
The atoi() function returns the following value:
- An equivalent integer value by interpreting the input string as a number only if the input string str is Valid.
- If the conversion is not valid, the function returns 0.
Note: In the case of an overflow the returned value is undefined.
Examples of atoi() in C
Example 1:
This example shows how to convert numbers in String form to their Integer values.
C
// C program to illustrate the use of atoi()
#include <stdio.h>
#include <stdlib.h>
int main()
{
// string to be converted
char strToConvert[] = "908475966";
// converting string using atoi()
int ConvertedStr = atoi(strToConvert);
// printing the Converted String
printf("String to be Converted: %s\n", strToConvert);
printf("Converted to Integer: %d\n", ConvertedStr);
return 0;
}
OutputString to be Converted: 908475966
Converted to Integer: 908475966
Explanation: The string "99898989" is converted to the corresponding integer value, and both the original string and the returned integer value are printed.
Example 2:
This example shows if the input string contains non-numeric characters so the function returns 0.
C
// C program to illustrate the use of atoi()
#include <stdio.h>
#include <stdlib.h>
int main()
{
// string to be converted
char strToConvert[] = "geeksforgeeks";
// converting string using atoi()
int ConvertedStr = atoi(strToConvert);
// printing the Converted String
printf("String to be Converted: %s\n", strToConvert);
printf("Converted to Integer: %d\n", ConvertedStr);
return 0;
}
OutputString to be Converted: geeksforgeeks
Converted to Integer: 0
Explanation: The string "geeksforgeeks" is attempted to be converted to an integer but the string contains non-numeric characters so the function returns 0.
How to Implement Your Own atoi() in C?
The equivalent of the atoi() function is easy to implement. One of the possible method to implement is shown below:
Approach
- Initialize res to 0.
- Iterate through each character of the string and update the res by multiplying it by 10 and keep adding the numeric value of the current digit. (res = res*10+(strg[i]-'0'))
- Continue until the end of the string.
- return res.
Implementation of atoi() in C
C
// C program to Implement Custom atoi()
#include <stdio.h>
int atoi_Conversion(const char* strg)
{
// Initialize res to 0
int res = 0;
int i = 0;
// Iterate through the string strg and compute res
while (strg[i] != '\0') {
res = res * 10 + (strg[i] - '0');
i++;
}
return res;
}
int main()
{
const char strg[] = "12345";
int value = atoi_Conversion(strg);
// print the Converted Value
printf("String to be Converted: %s\n", strg);
printf("Converted to Integer: %d\n", value);
return 0;
}
OutputString to be Converted: 12345
Converted to Integer: 12345
To learn more about different approaches, refer to the article - Write your own atoi()
Properties of atoi() Function
The atoi() function have different output for different input types. The following examples shown the behaviors of the atoi() function for different input values.
1. The atoi() function does not recognize decimal points or exponents.
C
// C program to illustrate the Property 1: Passing the
// decimal point string to atoi()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int res_val;
char inp_str[30];
// Initialize the input string
strcpy(inp_str, "12.56");
// Converting string to integer using atoi()
res_val = atoi(inp_str);
// print result
printf("Input String = %s\nResulting Integer = %d\n",
inp_str, res_val);
return 0;
}
OutputInput String = 12.56
Resulting Integer = 12
2. Passing invalid string to atoi() returns 0.
C
// C program to illustrate the Property 2: Passing Invalid
// string to atoi()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int res_val;
char inp_str[30];
// Initialize the input string
strcpy(inp_str, "geeksforgeeks");
// Converting string to integer using atoi()
res_val = atoi(inp_str);
// print result
printf("Input String = %s\nResulting Integer = %d\n",
inp_str, res_val);
return 0;
}
OutputInput String = geeksforgeeks
Resulting Integer = 0
3. Passing partially valid string results in conversion of only integer part. (If non-numerical values are at the beginning then it returns 0)
C
// C program to illustrate the Property 3: Passing Partially
// valid String to atoi()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int res_val;
char inp_str[30];
// Initializing the input string
strcpy(inp_str, "1234adsnds");
// Convert string to integer using atoi() and store the
// result in result_value
res_val = atoi(inp_str);
printf("Input String = %s\nResulting Integer = %d\n",
inp_str, res_val);
return 0;
}
OutputInput String = 1234adsnds
Resulting Integer = 1234
4. Passing string beginning with the character '+' to atoi() then '+' is ignored and only integer value is returned.
C
// C program to illustrate Property 4: Passing String
// beginning with the character '+' to atoi()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int res_val;
char inp_str[30];
// Initializing the input string
strcpy(inp_str, "+23234");
// Convert string to integer using atoi() and store the
// result in result_value
res_val = atoi(inp_str);
printf("Input String = %s\nResulting Integer = %d\n",
inp_str, res_val);
return 0;
}
OutputInput String = +23234
Resulting Integer = 23234
5. Passing string beginning with the character '-' to atoi() then the result includes '-' at the beginning.
C
// C program to illustrate Property 5: Passing String
// beginning with the character '-' passed to atoi()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int res_val;
char inp_str[30];
// Initializing the input string
strcpy(inp_str, "-23234");
// Convert string to integer using atoi() and store the
// result in result_value
res_val = atoi(inp_str);
printf("Input String = %s\nResulting Integer = %d\n",
inp_str, res_val);
return 0;
}
OutputInput String = -23234
Resulting Integer = -23234
Conclusion
- atoi() function converts numeric string to its corresponding integer value.
- To use the atoi() function in your C program include the stdlib.h header file.
- atoi() accepts only one string parameter as input.
- If an invalid string is passed as parameter, the function returns 0.
Similar Reads
atexit() function in C/C++
The function pointed by atexit() is automatically called without arguments when the program terminates normally. In case more than one function has been specified by different calls to the atexit() function, all are executed in the order of a stack (i.e. the last function specified is the first to b
3 min read
Functions in C++
A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
clock() function in C
The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be
2 min read
C String Functions
C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.Th
6 min read
main Function in C
The main function is the entry point of a C program. It is a user-defined function where the execution of a program starts. Every C program must contain, and its return value typically indicates the success or failure of the program. In this article, we will learn more about the main function in C.E
5 min read
fabs() Function in C
fabs() function of math.h header file in C programming is used to get the absolute value of a floating point number. This function returns the absolute value in double. Syntax: double fabs (double a); Parameter: It will take a single parameter which is to be converted to an absolute value. Return Va
2 min read
getx() function in C
The header file graphics.h contains getx() function which returns the X coordinate of the current position. Syntax : int getx(); Example : Explanation : Initially, the X coordinate of the current position is 0. On moving the coordinates using moveto() function, the X coordinate changes to 80. Below
2 min read
gety() function in C
The header file graphics.h contains gety() function which returns the Y coordinate of the current position. Syntax : int gety(); Example : Explanation : Initially, the Y coordinate of the current position is 0. On moving the coordinates using moveto() function, the Y coordinate changes to 50. Below
2 min read
fmod() Function in C
The fmod() function in C computes the floating-point remainder of the division of two numbers. It is part of the math library <math.h> in C and <cmath> in C++.fmod() in CThe fmod() function returns the remainder of the division of two floating-point numbers. It is particularly useful whe
3 min read
strtod() function in C/C++
The strtod() is a builtin function in C and C++ STL which interprets the contents of the string as a floating point number and return its value as a double. It sets a pointer to point to the first character after the last valid character of the string, only if there is any, otherwise it sets the poi
4 min read