How to convert an int to string in C++?


You can use the itoa function from C to convert an int to string. 

 example

#include<iostream>
int main() {
   int a = 10;
   char *intStr = itoa(a);
   string str = string(intStr);
   cout << str;
}

Output

This will give the output −

10

This will convert the integer to a string. In C++11, a new method, to_string was added that can be used for the same purpose. You can use it as follows −

Example

#include<iostream>
using namespace std;
int main() {
   int a = 10;
   string str = to_string(a);
   cout << str;
}

Output

This will give the output −

10

Updated on: 24-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements