Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

atoi() Function in C

C is one of the basic and used programming languages around the world. It is widely used because of many features like it is easy to implement, the syntax is simple, it provides many in-built functions, supports many libraries that can make the code easy to understand. The atoi function in c is one of the most used functions as it helps the programmer to save time and also reduces the length of the code. While moving further in this article we will learn all about atoi in c with its syntax, parameters, return types, and examples followed by its applications.

What is atoi() Function?

The atoi function in c is used to get the integer value from the string of characters. The input is nothing but a string of characters that will read the characters and stops when it encounters a character and that character is not considered to be a part of the number. It also stops when it encounters the null character at the end of the string. The atoi function in c down not supports the decimal numbers and exponents. The function is included in the library stdlib.h.
It will skip all the white space characters in the string. It will return the integer conversion of the given string.

Syntax of atoi() Function in C

The syntax of the atoi function in c is given below.

int atoi(const char *str);

Parameters of atoi() Function in C

The atoi function in c will have only one input parameter. The only input parameter passed to the atoi in c is a string. We should always pass the string at a constant. We pass the value of the input string as constant because the function only has to return the integer value and the function does not have to do anything with the string so to make sure the function does not change the value of the string we pass it as constant.

Return Value of atoi() Function in C

If the input is valid the atoi function in c will return the corresponding integer value of the given string. For the non-valid inputs, the atoi function in will return 0.
It works by reading the characters of the string one by one so the moment it sees a null character, or a non-ASCII value it will stop working and it will also skips all the white blank spaces at the start of the string.

There atoi function in c can work if the string follows the following conditions.

  • The strings that are made up of entirely ASCII digits i.e, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’.
  • Strings begin with the character’-’ and the reset string contains only the ASCII digits.
  • Strings begin with the character’+’ and the reset string contains only the ASCII digits.

The function will read the characters one by one and if till the end it does not find any digit it will return 0. Otherwise, it will return the number found before the occurrence of the first non-ASCII value.

Examples of atoi() Function in C

Below are the examples of the atoi function in C.

Example 1 of atoi() Function in C
In this example, we will see the example of two strings one containing the ASCII digits and one not.

Code Implementation

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>   
int main()  
{  
    int val;  
    char s1[20] = "12345";  
    val = atoi(s1);  
    printf("String value = %s\n", s1);  
    printf("Integer value = %d\n", val);  
    char s2[] = "PrepBytes";  
    val = atoi(s2);  
    printf("String value = %s\n", s2);  
     
}

Output

String value = 12345
Integer value = 12345
String value = PrepBytes
Integer value = 0

Explanation of the above example
In the above example we have two strings with s1 and s2. S1 contains the ASCII digits hence it shows the string and corresponding integer value and for the other string it will show the string and as it is non-ASCII so it will show 0.

Example 2 of atoi() Function in C
In this example, we will discuss the code implementation of other examples of the atoi function in c.

Code Implementation

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>   
int main()  
{  
    int val;  
    char s1[20] = "-4568";  
    val = atoi(s1);  
    printf("String value = %s\n", s1);  
    printf("Integer value = %d\n", val);  
    char s2[] = "1235xyz";  
    val = atoi(s2);  
    printf("String value = %s\n", s2);  
    printf("Integer value = %d\n", val);  

    return (0);  
}

Output

String value = -4568
Integer value = -4568
String value = 1235xyz
Integer value = 1235

Explanation of the above example
In the above example we first have a string with ‘-’ sign and the second string contains the ASCII digits and non-ASCII digits hence it returns the value in the value in the first and the value of the second string before the characters.

Applications of the atoi() Function in C

Below, we have discussed some of the applications of the atoi function in c.

  • It is very helpful in cases when we have to validate the input as sometimes we need to change the input from string to an integer.
  • It is used for command line arguments allowing the program to perform the desired operation.
  • It is used in data processing as we have to convert the data into its numerical form to analyze it.

Conclusion
In summary, the atoi function in C is a useful function for converting a string of characters representing a number to an actual integer value. It’s important to note that the function does not perform error checking, so it’s important to validate input strings before passing them to the function to avoid potential bugs and unexpected results.

Frequently Asked Questions

Here are some of the frequently asked questions about the atoi function in c.

1. Does the atoi function modify the input string?
No, the atoi function does not modify the input string.

2. Does the atoi function perform any error checking?
No, the atoi function does not perform any error checking. If the input string is not a valid integer, the function will return an unpredictable value.

3. What is the maximum value that can be returned by the atoi function?
The maximum value that can be returned by the atoi function depends on the maximum value that can be represented by an integer in the specific implementation of the C programming language being used.

4. What happens if the input string contains non-numeric characters?
The atoi function stops parsing the input string as soon as it encounters a non-numeric character. The resulting integer value will be the numeric portion of the input string that was successfully parsed.

5. What happens if the input string is empty?
If the input string is empty, the atoi function will return 0.

Leave a Reply

Your email address will not be published. Required fields are marked *