Convert Integer to String
Convert Integer to String in C++ (5 Programs)
C++ Convert Integer to String Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}Integer: 12345 String (method 1): 12345 String (method 2): 12345
Understanding Convert Integer to String
This program teaches you how to convert an integer into a string in C++. This conversion is essential when you need to display numbers as text, concatenate numbers with strings, write numbers to files, or format output. Understanding different conversion methods helps you choose the most appropriate approach for your specific needs.
---
1. What This Program Does
The program converts an integer (like 12345) into a string (like "12345"). This conversion is necessary because:
Example:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
int num = 12345;
---
4. Method 1: Using to_string() (Integer to String)
string str1 = to_string(num);
This is the most modern and recommended method in C++:
How it works:
Advantages:
Example:
int n = 12345;
string s = to_string(n); // s = "12345"
---
5. Method 2: Using stringstream
stringstream ss;
ss << num;
string str2 = ss.str();
This method uses stringstream to convert the integer:
How it works:
Advantages:
Example with formatting:
stringstream ss;
ss << fixed << setprecision(2) << 123.45;
string s = ss.str(); // s = "123.45"
Example with multiple values:
stringstream ss;
---
6. Other Methods (Mentioned but not shown in code)
Method 3: Using sprintf() (C-style)
char buffer[20];
sprintf(buffer, "%d", num);
string str3 = buffer;
#include <cstdio>Method 4: Using itoa() (Non-standard, C-style)
char buffer[20];
itoa(num, buffer, 10); // 10 = base 10 (decimal)
string str4 = buffer;
#include <cstdlib>Method 5: Manual Conversion
string str5 = "";
int temp = num;
if (temp == 0) str5 = "0";
else {
bool negative = temp < 0;
if (negative) temp = -temp;
while (temp > 0) {
str5 = char('0' + temp % 10) + str5;
temp /= 10;
}
if (negative) str5 = "-" + str5;
}
---
7. How Manual Conversion Works (Step-by-Step)
Understanding manual conversion helps you understand the concept:
For integer 12345:
The process:
---
8. Displaying Results
The program prints:
Output:
Both methods produce the same string value.
---
cout << "Integer: " << num << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;9. When to Use Each Method
-
to_string()
: Best for most cases - simple, safe, modern C++
-
stringstream
: Best when you need formatting or combining multiple values
-
sprintf()
: Only for legacy code or when you need C compatibility
-
itoa()
: Avoid - not standard, not portable
-
Manual
: For learning or when you need custom conversion logic
---
10. Formatting Considerations
With to_string()
: Basic conversion, no formatting options
string s = to_string(123.456); // "123.456000" (default precision)
With stringstream
: Full formatting control
stringstream ss;
ss << fixed << setprecision(2) << 123.456;
string s = ss.str(); // "123.46"
For integers
: to_string() is usually sufficient
For floats/doubles
: stringstream gives better control over decimal places
---
11. Performance Considerations
-
to_string()
: Fastest and most efficient for simple conversions
-
stringstream
: Slightly slower but more flexible
-
sprintf()
: Fast but less safe (buffer management)
-
Manual
: Slowest but most educational
For most applications, the performance difference is negligible. Choose based on code clarity and requirements.
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning how to work with different data types, format output, and combine numbers with text in C++ programs.
Let us now understand every line and the components of the above program.
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.
Practical Learning Notes for Convert Integer to String
This C++ program is part of the "String Conversion Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.