Convert Int To String In C++

In C++, integer and string are two data types. An integer is mainly utilized to show the numbers, whereas the string is used to save a collection of characters. In C++, converting an integer (int) to a string (std::string) is a common operation. It is especially useful when formatting output, logging, or preparing data for display or file storage.

There are several methods that can help to convert an int to a string in C++. Some of them are as follows:

  • Using std::stringstream
  • Using std::to_string (C++11 and later)
  • Using Boost or other libraries
  • Using sprintf or snprintf (C-style)

Now, we will discuss these methods one by one.

Convert an integer into a string Using Stringstream Class

In C++ , the stringstream class is a stream class that is defined in the header file. It is a stream class used to perform the input-output operations on string-based streams.

Syntax

It has the following syntax:

Example

std::stringstream ss;     // stringstream object

ss << num;    // Insert the integer into the stream

std::string str = ss.str();

The following are the operators used to insert or extract the data:

  • Operator >>: It extracts the data from the stream.
  • Operator <<: It inserts the data into the stream.

Let's understand the concept of operators through an example.

  • In the below statement, the << insertion operator inserts the 100 into the stream.
  • Example
    
    stream1 << 100;
    
  • In the below statement, the >> extraction operator extracts the data out of the stream and stores it in 'i' variable.
  • Example
    
    stream1 >> i;
    

    Example to convert int to string Using Stringstream Class

Let's take an example to demonstrate how to convert int to string using stringstream class in C++.

Example

Example

#include <iostream>  

#include<sstream>  

using namespace std;    //using standard namespace

int main() {  //Main Function

  int k;  

    cout<<"Enter an integer value: ";  

    cin>>k;  

  stringstream ss;

  ss<<k;    //insert integer into stream

  string s = ss.str();  

    cout<<"\n"<<"An integer value is: "<<k<<"\n";  

    cout<<"String representation of an integer value is: "<<s;   

}

Output:

Output

Enter an integer value: 45

An integer value is: 45

String representation of an integer value is: 45

Explanation

In this example, we have created a k variable, and want to convert the value of k into a string value. After that, we use the stringstream class, which is used to convert the k integer value into a string value.

Convert an integer into a string Using to_string method

In C++, the to_string method accepts a single integer and converts the integer value or other data type value into a string.

Syntax

It has the following syntax:

Example

string to_string(int value);

C++ Example to convert int to string using to_string method

Let's take an example to demonstrate how to convert int to string using to_string function in C++.

Example

Example

#include <iostream>  

#include<string>  

using namespace std;   //using standard namespace

int main()  //Main Function

{  

int i=11;   //intialize integer value

float f=12.3;  //intialize integer value

string str= to_string(i);  

string str1= to_string(f);  

cout<<"string value of integer i is: "<<str<<"\n";  

cout<<"string value of f is: "<< str1;  

}

Output:

Output

string value of integer i is: 11

string value of f is: 12.300000

Explanation

In this example, we have taken the to_string function that converts the int and float values into their corresponding string representations. It provides an easy and understandable way to handle numeric to string conversions in C++.

Convert an integer into a string Using a boost.lexical cast

In C++, the boost.lexical cast provides a cast operator, i.e., boost.lexical_cast, which converts the string value into an integer value or other data type value and vice versa.

Syntax

It has the following syntax:

Example

std::string str = boost::lexical_cast<std::string>(integer_variable);

C++ Example to convert an integer to string using a boost.lexical cast

Let's take an example to demonstrate how to convert int to string using boost.lexical cast in C++.

Example

Example

#include <iostream>  

#include <string>

#include <boost/lexical_cast.hpp>  

using namespace std;   //using standard namespace

using boost::lexical_cast;

    int main()  //Main Function

  {  

    int i=11;  //initialize integer value

    string str = boost::lexical_cast<string>(i);  

    cout<<"string value of integer i is :"<<str<<"\n";  

}

Output:

Output

string value of integer i is :11

Explanation

In the above example, we have converted the value of 'i' variable into a string value by using lexical_cast function.

Convert an integer into string Using sprintf or snprintf (C-style)

In C++, another way to convert an integer to a string is by using the C-style functions sprintf or snprintf, which are defined in the <cstdio> header. These functions are part of the C standard library and provide formatted string output, which can also be used to perform type conversions, including converting integers to character arrays (C-style strings), which can be converted to std::string.

Syntax

It has the following syntax:

Example

sprintf(buffer, "%d", num);  // Or use snprintf(buffer, sizeof(buffer), "%d", num);

std::string str(buffer);

C++ Example to convert an int to string using sprintf/snprintf

Let us take an example to illustrate how to convert an int to a string using sprintf/snprintf in C++.

Example

Example

#include <cstdio>

#include <iostream>

using namespace std;   //using standard namespace

int main() {   //Main Function

    int num = 123;  //initialize integer number

    char buffer[20];

    std::sprintf(buffer, "%d", num);

    std::string str = buffer;

    std::cout << "Converted string: " << str << std::endl;

    return 0;

}

Output:

Output

Converted string: 123

Explanation

In this example, we use sprintf to convert an integer into a C-style string (char) and then assign it to a C++ std::string. It is a traditional C-style approach but works effectively in C++ as well.

When and Why Do We Convert int to string in C++?

In C++, an int is used to store numbers, and a string is used to store text. Sometimes, we need to change a number (int) into text (string). It is helpful when we want to:

  • Join the number with other text (concatenation)
  • Format the number nicely for display
  • Save the number as text in a file
  • Show the number on the screen

One way to do this in C++ is by using the stringstream class. It allows us to treat numbers like text. We put the number into the stream, and then read it back as a string. It is useful when we need to:

  • Pass the number as text to a function
  • Store it in a data structure that uses strings
  • Conclusion

In C++, converting an int to a string is a straightforward task with several approaches depending on the C++ version and project needs. For modern C++ developers, std::to_string is the clear and concise choice to convert an int to a string. Understanding other methods like stringstream and sprintf is still valuable, especially when working with legacy code or systems with limited standard library support.

C++ Convert an Int to String MCQs

1) Which of the following is the most modern and simplest way to convert an int to string in C++11 and later?

  • sprintf
  • std::to_string
  • std::stoi
  • atoi

2) What is the main purpose of using stringstream for int-to-string conversion in C++?

  • To treat numbers like streams of characters
  • To store multiple integers in a string
  • To perform mathematical operations on strings
  • To convert lowercase to uppercase

3) Which of the following methods is used in C-style programming to convert an int to a string?

  • std::to_string
  • std::stringstream
  • boost::lexical_cast
  • sprintf

4) Why would a programmer convert an int to a string in C++?

  • To perform arithmetic operations
  • To use it in a loop
  • To display it with other text or save it as text
  • To convert it to a float

5) What does the boost::lexical_cast<std::string>(int) function do in C++?

  • Converts a string to integer
  • Converts a float to string
  • Converts an int to a string
  • Converts a character to string

Input Required

This code uses input(). Please provide values below: