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


Output

Input 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, "w3wiki");
  
    // 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 = w3wiki
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;
}


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




// 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;
}


Output

Input 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;
}


Output

Input String = -23234
Resulting Integer = -23234


atoi() Function in C

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.

Similar Reads

Syntax of atoi() Function

int atoi(const char *strg);...

Parameters

strg: The string that is to be converted to an integer....

Return Value

The atoi() function returns the following value:...

Examples of atoi() in C

Example 1:...

How to Implement Your Own atoi() in C?

...

Properties of atoi() Function

...

Conclusion

The equivalent of the atoi() function is easy to implement. One of the possible method to implement is shown below:...

Contact Us