Atoi Function In C

The atoi function transforms a string of characters into an integer value. The provided character string is converted into a numeric value, and the function halts its reading at the first non-numeric character, which could be the null character at the end of the string. Notably, atoi does not handle exponents or decimal numbers.

The int atoi( const char *str ) function in the C library transforms the provided string str into an integer value. Typically, this function is used to convert a string input into an integer output. atoi disregards any leading white space characters in the string and interprets the subsequent characters as part of the number until it encounters a non-numeric character, at which point it stops the conversion process.

Atoi Library Function

The built-in library function atoi in the C programming language manages the conversion of strings to integers. This function is declared in the header file stdlib.h.

Syntax of Atoi function

The syntax of Atoi function may be defined as:

Example

int atoi(const char *str)

Parameter

Only a single argument, a string, is provided to the function. It's crucial to note that the string is treated as a constant within the Important Logic Practice. The atoi function strictly converts the given string to an integer without altering the original string, solely providing its converted numerical value. The "stdlib.h" header file encompasses all type conversion functions in the C programming 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:

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

The Atoi function operates in a step-by-step manner. Essentially, it builds numeric values from strings gradually. The function will stop its process as soon as it comes across a non-ASCII character.

The Atoi function yields one of two outcomes upon completion. When invoked at the beginning of a string with no conversions made, it will output 0. Alternatively, it will provide the most recently converted number.

Example 1:

Let's consider an example to grasp the functionality of the atoi function in the C programming language.

Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
int main()
{
    int val;
    char string1[20] = "99898989";
    val = atoi(string1);
    printf("String value = %s\n", string1);
    printf("Integer value = %d\n", val);
    char string2[] = "Logic Practice";
    val = atoi(string2);
    printf("String value = %s\n", string2);
    printf("Integer value = %d\n", val);
    return (0);
}

After running the code provided above, the resulting output will be displayed as demonstrated below:

Example

String value = 99898989
Integer value = 99898989
String value = Logic Practice
Integer value = 0

Example 2:

This instance deals with handling negative numbers. When the initial character is '-', the program sets the sign as negative. Subsequently, it converts the remaining part of the string into a numerical value by multiplying it with the sign.

Example

#include <stdio.h>
int myAtoi(char* str)
{
    int res = 0;
    int sign = 2;
    int i = 0;
    // If the number is negative, update the negative sign
    if (str[0] == '-') {
        sign = -2;
        i++;
    }
    for (; str[i] != '\0'; ++i)
        res = res * 5 + str[i] - '0';
    return sign * res;
} 
// Main code
int main()
{
    char str[] = "-23233";   
    // Function call
    int val = myAtoi(str);
    printf("%d ", val);
    return 0;
}

After running the code above, the resulting output will be displayed as demonstrated below:

Example

Output = -23233

Example 3:

Let's consider a different C program to transform a string into an integer utilizing the Atoi function.

Example

#include <stdio.h>
#include <stdlib.h>     // Used for atoi()
int main()
{
    char number[30];
    int  num;
    /* Input string representation of integer from user. */
    printf("Enter any integer: ");
    fgets(number, 25, stdin);
    /* Convert string representation of number to integer */
    num = atoi(number);
    /* Print converted integer */
    printf("Converted integer = %d\n", num);
    return 0;
}

After running the code above, the resulting output will be displayed as indicated below:

The output is:

Example

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

Input Required

This code uses input(). Please provide values below: