Convert Integer to String

Convert Integer to String in C++ (5 Programs)

BeginnerTopic: String Conversion Programs
Back

C++ Convert Integer to String Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
    int num = 12345;
    
    // Method 1: Using to_string()
    string str1 = to_string(num);
    
    // Method 2: Using stringstream
    stringstream ss;
    ss << num;
    string str2 = ss.str();
    
    cout << "Integer: " << num << endl;
    cout << "String (method 1): " << str1 << endl;
    cout << "String (method 2): " << str2 << endl;
    
    return 0;
}
Output
Integer: 12345
String (method 1): 12345
String (method 2): 12345

Understanding Convert Integer to String

This program demonstrates 5 different methods to convert an integer to a string: using to_string(), stringstream, sprintf(), itoa(), and manual conversion.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents