C++ atoi

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The C++ atoi() is a predefined function that is used for converting a string str to the integer type. Sometimes we need to convert the string values to the int type, here the c++ atoi() function comes into use. The atoi() function in c++ is found in the cstdlib header file.

Syntax of C++ atoi() Function

The syntax of the c++ atoi() function is as follows:

Parameters of C++ atoi() Function

The parameter of the c++ atoi() function is as follows:

The c++ atoi() function accepts the str as the parameter. This parameter represents the string argument. This is the argument that the atoi() function converts to the int type.

Return Value of C++ atoi() Function

The return value of the c++ atoi() function is as follows:

  • Return Value: int The c++ atoi() function returns the equivalent integer value of the string that the function accepts as the parameter. The function only returns the "int" type value only if the given parameter is a valid input.

Exceptions of C++ atoi() Function

The exceptions of the c++ atoi() function are as follows: As we know, the c++ atoi() function accepts only a string as the parameter, that can be represented in the form of an integer. If the string is Null or contains any other character instead of an integer value then the c++ atoi() function will give undefined behavior.

How Does C++ atoi() Function Works?

The atoi() function is used to convert the string and the char data type into the integer type. The function first tries to remove the unwanted white space from the string and then checks whether the initial character indicates a plus or minus to find whether the number generated should be a positive or negative integer. The c++ atoi() function is found in the header file cstdlib. This function is used for converting a string value to an integer value. The c++ atoi() function accepts the string value as the parameter only if the string value is a valid input. The function does this to make it possible to interpret the numerical sets of values. It may be possible that the string also contains special characters.

In this case, the function simply ignores these special characters. These special characters do not affect the performance in any way. After doing all these, if the c++ atoi() function passes the valid argument that is the string then the function will be able to return the integer value for that specific string value. One more thing we need to remember is that the converted integer value is out of range means the represented value is in int data type, and it causes some undefined behaviour; it throws the exceptions.

How to Create Your own atoi() Function

In the above section, we have discussed what is a c++ atoi() function. Now let us discuss how the c++ atoi() function can be created. There are various approaches by which we can create the c++ atoi() function. Let us discuss them one by one with suitable examples, output, and explanations where ever necessary.

Approach 1.

Given below is an example of the implementation of the c++ atoi() function that will convert the string to int type without any special case.

  • First, the result is initialized as 0.
  • Then we will start with the first character for updation and after that, we will update the result corresponding to every character.
  • Then we will update the answer and the resulting answer will be in the form of result = result*10+(s[i] -0), for each and every character of the given string.

Output:

In the above example, the time complexity of the program is O(n); here n represents the length of the given string.

Approach 2.

In this approach, we will see how the function c++ atoi() handles the negative numbers.

  • If the given first character is negative (-), then the function stores this negative sign. Then the function starts to convert the rest of the string into integer type. This conversion takes place according to the earlier simpler example.

Output:

In the above example, the time complexity of the program is O(n); here n represents the length of the given string.

Approach 3.

In this approach, we will see the case where the function handles four corner cases.

  • First, discard all leading whitespaces.
  • The sign of the number.
  • Overflow
  • Invalid Input

To remove the leading whitespaces run a loop until a character of the digit is reached. If the number is greater than or equal to INT_MAX/10. Then return INT_MAX if the sign is positive and return INT_MIN if the sign is negative. The other cases are handled in previous approaches.

Output:

In the above example, the time complexity of the c++ atoi() function is O(n). And the space complexity of the c+ atoi() function is O(1). In this case, we do not need any extra space.

Examples of C++ atoi() Function

Now let us see an example of the c++ atoi() function in c++ program.

Output:

Explanation: In the above example, We have used the c++ atoi() function to convert the string into the int type. First, we declared the integer named variable val. Then we declared the character string char str whose size is 20. Then, we used the c++ atoi() function to convert the given two strings to the int type.

Conclusion

  • The c++ atoi() is a predefined function that is used for converting a string str to integer type and this function is found in the header file cstdlib.
  • The syntax of the c++ atoi() function is int atoi(const char strn) and c++ atoi() function accepts the str as the parameter. This parameter represents the string argument.
  • If the string is Null or contains any other type of integer value then the c++ atoi() function does not work and becomes negligible in the program and the function fails to throw an error or an exception.
  • We can create our own atoi() function by considering some corner cases.

Read More: