How to Convert int to string in C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

«Int to string» in C++: Why Does This Question Torment Even Experienced Developers?

\n

Imagine this: you are writing a C++ program that needs to display a number on the screen and then append text to it — for example, «Result: 42». Or you need to save a user ID (int) to a JSON file as a string. It seems simple enough, right? But here's the catch — in C++, you cannot simply write string s = 42;. The compiler will throw an error.

\n

The question «How can I convert int to string in C++?» is one of the most popular on Stack Overflow (hundreds of thousands of views). And this is no coincidence. Unlike Python or JavaScript, where everything «magically converts itself», C++ requires explicit type conversion. But don't worry! In this article, we will cover 5 proven methods, from classics to modern C++20. You will learn which method is faster, which is more convenient, and when to use each one.

\n

Let's go!

\n\n

Method #1: std::to_string — Modern and Simple (C++11 and later)

\n

Starting with C++11, the standard library introduced the std::to_string function. This is the simplest and most readable way to convert int to string in C++.

\n
#include <iostream>\n#include <string>\n\nint main() {\n    int number = 2024;\n    std::string str = std::to_string(number);\n    std::cout << "String: " << str << std::endl;  // Output: String: 2024\n    return 0;\n}
\n

Pros:

\n
    \n
  • Short and clear — one line of code
  • \n
  • Supports int, long, float, double, and other numeric types
  • \n
  • No need to include additional libraries (except <string>)
  • \n
\n

Cons:

\n
    \n
  • No control over formatting (number of digits, leading zeros)
  • \n
  • Slower than low-level methods if called millions of times
  • \n
\n

When to use: In 90% of cases. For ordinary tasks where no specific output format is needed, std::to_string is the ideal choice.

\n\n

Method #2: std::stringstream — Flexible Like a Swiss Army Knife

\n

If you need not just to convert a number but also to format it (add spaces, change the number base), std::stringstream comes to the rescue. This is a class from the <sstream> library that works like a «smart string».

\n
#include <iostream>\n#include <sstream>\n#include <string>\n\nint main() {\n    int number = 255;\n    std::stringstream ss;\n    ss << "Value: " << number << " (hexadecimal: " << std::hex << number << ")";\n    std::string result = ss.str();\n    std::cout << result << std::endl;\n    // Output: Value: 255 (hexadecimal: ff)\n    return 0;\n}
\n

Pros:

\n
    \n
  • Full control over formatting (std::hex, std::oct, std::setw, std::setprecision)
  • \n
  • Can concatenate strings and numbers on the fly
  • \n
\n

Cons:

\n
    \n
  • Slower than std::to_string due to creating a stringstream object
  • \n
  • Requires more code for simple conversion
  • \n
\n

When to use: When you need complex formatting or conversion to different number bases.

\n\n

Method #3: sprintf and snprintf — The Old-School C Method

\n

An old but reliable method inherited from the C language. The sprintf function writes a formatted string into a char array. It can also be used in C++, but you need to be careful about buffer overflow.

\n
#include <iostream>\n#include <cstdio>  // for sprintf\n#include <cstring> // for strlen\n\nint main() {\n    int number = 42;\n    char buffer[50];\n    sprintf(buffer, "%d", number);\n    std::string str(buffer);\n    std::cout << "Result: " << str << std::endl;\n    return 0;\n}
\n

Important:

Blogs

Book Recommendations