How to convert an integer to string in C++ STL?

C++ STL | converting an integer to string: In this article, we are going to see how we can convert an integer to string in C++?
Submitted by Radib Kar, on February 27, 2019

Converting integer to string

It's often needed to convert integer datatype to string variable. C++ has its own library function to_string(), which converts any numerical value to its corresponding string type. For example, 123 converts to "123".

First 123 is an integer, whereas "123" is string value
int i = 123;
string s = to_string(i);

to_string() Function details


    string to_string(int/long/long long);

Parameter: numerical value

Return type: string


    Like we define and declare,

    int i=5;
    string s=to_string(i);
        cout<<"converted to string";
        cout<<"Failed to convert.";

Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.

Header file needed:

    #include <string>
    #include <bits/stdc++.h>

C++ program to convert an integer to string

#include <bits/stdc++.h>
using namespace std;

int main(){
	int n;

	cout<<"Input integer to convert\n";

	string s=to_string(n);

	cout<<"Converted to string: "<<s<<endl;

	return 0;


Input integer to convert
Converted to string: 23

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.