String to int Conversion Using atoi()

The atoi() function in C++ takes a character array or string literal as an argument and returns its value in an integer. It is defined in the <stdlib.h> header file. This function is inherited by C++ from C language so it only works on C style strings i.e. array of characters.

If you observe atoi() a little closer you will find out that it stands for:

Breakdown of atoi() in simple terms

Example:

C++




// C++ program to demonstrate the
// functioning of the atoi() function
#include <iostream>
#include <stdlib.h>
using namespace std;
 
int main()
{
    char* str1 = "141";
    char* str2 = "3.14";
 
    int res1 = atoi(str1);
    int res2 = atoi(str2);
 
    cout << "atoi(" << str1 << ") is " << res1 << "\n";
    cout << "atoi(" << str2 << ") is " << res2 << "\n";
 
    return 0;
}


Output

atoi(141) is 141 
atoi(3.14) is 3 

stoi() vs atoi()

stoi()

atoi()

1. stoi() is added in C++ 11 1. atoi() is a legacy C-style function
2. stoi() works for both C++ strings and C-style strings (character array and string literal) 2. atoi() works only for C-style strings (character array and string literal)
3. stoi() can take up to three parameters, the second parameter is for 
starting index and the third parameter is for the base of the input number.
3. atoi()takes only one parameter and returns an integer value

4. Syntax:

int stoi (const string&  str, size_t* index = 0, int base = 10); 

4. Syntax:

int atoi (const char * str);

Convert String to int in C++

Converting a string to int is one of the most frequently encountered tasks in C++. As both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. Conversion is mostly done so that we can convert numbers that are stored as strings.

Example:

str=”191″

num=191

There are 5 significant methods to convert strings to numbers in C++ as follows:

  1. Using stoi() function
  2. Using atoi() function
  3. Using stringstream
  4. Using sscanf() function
  5. Using for Loop
  6. Using strtol() function

Similar Reads

1. String to int Conversion Using stoi() Function

The stoi() function in C++ takes a string as an argument and returns its value in integer form. This approach is popular in current versions of C++, as it was first introduced in C++11....

2. String to int Conversion Using atoi()

...

3. String to int Conversion Using stringstream Class

The atoi() function in C++ takes a character array or string literal as an argument and returns its value in an integer. It is defined in the header file. This function is inherited by C++ from C language so it only works on C style strings i.e. array of characters....

4. Converting String to int Using sscanf()

...

5. Using For Loop Convert Strings into int

The stringstream class in C++  allows us to associate a string to be read as if it were a stream. We can use it to easily convert strings of digits into ints, floats, or doubles. The stringstream class is defined inside the header file....

6. String to int Conversion Using strtol()

...

Contact Us