Javatpoint Logo
Javatpoint Logo

Atoi() function in C

In this article, we are going to discuss the atoi() function in c with their examples.

What is Atoi()?

The atoi() function converts an integer value from a string of characters. The input string is a character string that may be turned into a return type numeric value. The function stops reading the input string when it encounters the first character that it does not consider part of a number. It may be the null character at the string ends. The atoi() function doesn't support the exponents and decimal numbers.

The function int atoi(const char *str) in the C library changes the string argument str to an integer. Generally, the function converts a string argument to an integer. The atoi() function skips all the white space characters at the starting of the string. It also converts the characters as the number part and stops when it finds the first non-number character.

Atoi() Library Function

The pre-defined library function atoi() in C programming handles string to integer conversion. The function is specified in the header file stdlib.h.

Syntax of Atoi() function

The syntax of Atoi() function may be defined as:


Only one parameter, a string, is passed to the function. The important point to remember that the string is received as a constant. The atoi() function never changes a received string and only returns its transformed integer value. The header file "stdlib.h" supports all typecasting functions in the C language.

Return Value

If str is a valid input, the function returns the integer number equal to the passed string number. If str has no valid input, the functions return zero value. In general, strings can be effectively converted to numbers include the following parameters:

  1. Strings made up entirely of ASCII digits '0123456789'.
  2. Strings that begin with the character '+' and are made up entirely of ASCII
  3. Strings made up entirely of ASCII digits and beginning with the character '-'.

The Atoi() function works incrementally. In other words, it constructs numbers that are represented by strings one by one. The function only breaks when it encounters a non-ASCII.

The Atoi() function returns one of two possible values after breaking. If the function is called at the start of the string and no values have been transformed, it returns 0. Otherwise, it would return the recent number it has.

Example 1:

Let's take an example to understand the atoi() function in C.

Output: After executing this above code, we will get the output as shown below:

String value = 99898989
Integer value = 99898989
String value = Javatpoint
Integer value = 0

Example 2:

This example manages the negative numbers. If the first character is '-', place the sign as negative and then transform the remainder of the string to a number by multiplying the sign by it.

Output: After executing this above code, we will get the output as shown below:

Output = -23233

Example 3:

Let's take another C program to convert the string to integer using Atoi () function.

Output: After executing the above code, we will get the output as shown below:

The output is:

Enter any integer = abc
Converted integer = 0
Enter any integer = 12221
Converted integer = 12221
Enter any integer = 1.1225112
Converted integer = 1

Youtube For Videos Join Our Youtube Channel: Join Now


  • Send your Feedback to

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA