There are two common methods to convert strings to numbers:
- Using stringstream class or sscanf()
stringstream() : This is an easy way to convert strings of digits into ints, floats or doubles. Following is a sample program using a stringstream to convert string to int.// A program to demonstrate the use of stringstream#include <iostream>#include <sstream>usingnamespacestd;intmain(){string s ="12345";// object from the class stringstreamstringstream geek(s);// The object has the value 12345 and stream// it to the integer xintx = 0;geek >> x;// Now the variable x holds the value 12345cout <<"Value of x : "<< x;return0;}chevron_rightfilter_noneOutput:
Value of x : 12345
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss << myString; or ss << myCstring; or ss << myInt;, or float, or double, etc. // and we can read from it: ss >> myChar; or ss >> myCstring; or ss >> myInt;
To summarize, stringstream is a convenient way to manipulate strings.
sscanf() is a C style function similar to scanf(). It reads input from a string rather that standard input.
#include<stdio.h>intmain(){constchar*str ="12345";intx;sscanf(str,"%d", &x;);printf("\nThe value of x : %d", x);return0;}chevron_rightfilter_noneOutput:
Value of x : 12345
Similarly we can read float and double using %f and %lf respectively.
- String conversion using stoi() or atoi()
stoi() : The stoi() function takes a string as an argument and returns its value. Following is a simple implementation:// C++ program to demonstrate working of stoi()// Work only if compiler supports C++11 or above.#include <iostream>#include <string>usingnamespacestd;intmain(){string str1 ="45";string str2 ="3.14159";string str3 ="31337 geek";intmyint1 = stoi(str1);intmyint2 = stoi(str2);intmyint3 = stoi(str3);cout <<"stoi(\""<< str1 <<"\") is "<< myint1 <<'\n';cout <<"stoi(\""<< str2 <<"\") is "<< myint2 <<'\n';cout <<"stoi(\""<< str3 <<"\") is "<< myint3 <<'\n';return0;}chevron_rightfilter_noneOutput:
stoi("45") is 45 stoi("3.14159") is 3 stoi("31337 geek") is 31337atoi() : The atoi() function takes a character array or string literal as an argument and returns its value. Following is a simple implementation:
// For C++11 above#include <iostream>#include <cstdlib>usingnamespacestd;intmain(){constchar*str1 ="42";constchar*str2 ="3.14159";constchar*str3 ="31337 geek";intnum1 =atoi(str1);intnum2 =atoi(str2);intnum3 =atoi(str3);cout <<"atoi(\""<< str1<<"\") is "<< num1 <<'\n';cout <<"atoi(\""<< str2<<"\") is "<< num2 <<'\n';cout <<"atoi(\""<< str3<<"\") is "<< num3 <<'\n';return0;}chevron_rightfilter_noneOutput:
atoi("42") is 42 atoi("3.14159") is 3 atoi("31337 geek") is 31337stoi() vs atoi()
- atoi() is a legacy C-style function. stoi() is added in C++ 11.
- atoi() works only for C-style strings (character array and string literal), stoi() works for both C++ strings and C style strings
- atoi() takes only one parameter and returns integer value.
int atoi (const char * str);
stoi() can take upto three parameters, the second parameter is for starting index and third parameter is for base of input number.
int stoi (const string& str, size_t* index = 0, int base = 10);
Similarly, for converting String to Double, atof() can be used. The above function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.
Exercise
Write your won atof() that takes a string (which represents an floating point value) as an argument and returns its value as double.
Reference:
http://www.cplusplus.com/reference/string/stoi/
http://www.cplusplus.com/reference/sstream/stringstream/
http://www.cplusplus.com/reference/cstdlib/atoi/
This article is contributed by Siffi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Converting string to number and vice-versa in C++
- Number of common base strings for two strings
- Search strings with the help of given pattern in an Array of strings
- C function to Swap strings
- How to convert C style strings to std::string and vice versa?
- Why "&" is not used for strings in scanf() function?
- How to write long strings in Multi-lines C/C++?
- Output of C++ programs | Set 29 (Strings)
- C++ Program to concatenate two strings using Operator Overloading
- Strings in C
- <strings> library in C++ STL
- Print all distinct circular strings of length M in lexicographical order
- Program to check if two strings are same or not
- Generate all the binary strings of N bits
- C++ program to compare two Strings using Operator Overloading
- Strings in C++ and How to Create them?
- Processing strings using std::istringstream
- Arrays and Strings in C++
- Storage for Strings in C
- Array of Strings in C++ (5 Different Ways to Create)

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
