atoi() Function in C (original) (raw)

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.

atoi-in-c

C++ `

#include <stdio.h> #include <stdlib.h>

int main() { char str[] = "12345"; int num;

// Convert string to integer
num = atoi(str);

printf("The integer value is: %d\n", num);
return 0;

}

`

Output

The integer value is: 12345

Return Value

The atoi() function returns the following value:

**Note: In the case of an overflow the returned value is undefined.

This example shows if the input string contains non-numeric characters so the function returns 0.

C `

#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;

}

`

Output

String 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:

Implementation of atoi() in C

C `

#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;

}

`

Output

String 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 `

#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;

}

`

Output

Input String = 12.56 Resulting Integer = 12

**2. Passing invalid string to atoi() returns 0.

C `

#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;

}

`

Output

Input 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 `

#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;

}

`

Output

Input String = 1234adsnds Resulting Integer = 1234

**4. Passing string beginning with the character '+' to atoi() then '+' is ignored and only integer value is returned.

C `

#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;

}

`

Output

Input String = +23234 Resulting Integer = 23234

**5. Passing string beginning with the character '-' to atoi() then the **result includes '-' at the beginning.

C `

#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;

}

`

Output

Input String = -23234 Resulting Integer = -23234