How to Convert an Int to a String in C++ – Integer Cast Tutorial
Ihechikara Vincent Abba
Type casting or type conversion is the process of converting a variable from one data type to another.
Type casting can be done implicitly or explicitly.
Implicit type casting gets done automatically via the compiler, while explicit type casting is done by the developer.
In this article, you’ll learn how to convert an integer to a string in C++ using the stringstream class and the to_string() method.
How to Convert an Int to a String in C++ Using the stringstream Class
Using the stringstream class, we can convert an integer to a string.
Here’s an example to help you understand why you’d need to convert an integer to a string:
In the example above, we created an int variable with a value of 20.
When we tried to concatenate that value in a string, we got an error saying «invalid operands of types. «.
The error was raised because we tried to perform an operation using two incompatible variable types. The solution would be to convert one variable and make it compatible with the other.
The stringstream class has insertion ( << ) and extraction ( >> ) operators.
The insertion operator is used to pass a variable to the stream. In our case, to pass an integer to the stream.
The extraction operator is used to give out the modified variable.
In other words, a stringstream object would take in a data type, convert it to another data type, and assign the new data type to a variable.
Here’s an example:
In the code above, we created a stringstream object called stream . Note that you must include the stringstream class before you can use it: include <sstream> .
We then inserted the integer into the stream: stream << age; .
After that, we created a new variable called age_as_string . This variable will store the string variable that will be extracted from the stream.
Lastly, we extracted the string type of the integer and stored it in the variable created above: stream >> age_as_string; .
Now we can concatenate the strings and get the desired result:
How to Convert an Int to a String in C++ Using the to_string() Method
You can use the to_string() method to convert int, float, and double data types to strings.
Here’s an example:
In the code above, we passed in the age variable to the to_string() : string age_as_string = to_string(age); .
This converted the age variable to a string. Just like the example in the last section, we can now use the variable as a string:
Summary
In this article, we talked about the different ways of converting an integer to a string in C++.
The examples showed how to use the stringstream class and the to_string() method to convert an integer to a string.
Convert Int to String in C
For that purpose, we will discuss quick and very beginner-friendly methods of converting an integer into a string in the C programming language.
Method 1 – Convert Int to String Using the Sprintf Function
The Sprintf function is one of the functions you can use to convert an integer value into a string. As the name suggests, the function will take any value and print it into a string.
It is very similar to the printf function. But instead of printing the value into the standard out, the function returns a formatted string which we can store in a variable and use later.
The function syntax is as shown below:
The function accepts three main parameters:
- str – this specifies a pointer to a char data type.
- format – the format parameter allows you to specify the type of output with a placeholder.
- args – this parameter specifies the integer values in which to convert into a string.
You will notice that the function returns an int type. This is because it returns a formatted string which is discarded, and a value of -1 if an error occurs.
Let us see this function in action.
int main ( ) {
int var = 100 ;
char int_str [ 20 ] ;
In the code above, we start by importing the necessary header files. For the sprint() function, we require the standard input and output header file only.
We then open the main function and declare two variables. The first is the integer value that we wish to convert to a string.
The next variable is the character array that we will use to store the string value once we convert the int into a string.
We then use the sprint function and pass the char type, the format, and the int as the parameters.
Finally, we can print the resulting string using the printf function. The resulting output is a shown:
$ gcc to_string. c
Method 2 – Convert Int to String With itoa() Function (non-standard)
There is another non-standard function in C that you can use to convert an int to a string. It is a simple type-casting function. It first appeared in the C Programming Language book.
However, as the book states, this function is non-standard and does not handle negative integers very well.
Since it is non-standard, attempting to compile the function will heavily depend on your OS and if the function is available.
However, we are developers, and experimenting is in our nature.
The following syntax shows how the itoa() function works.
The function takes three main parameters:
- num – this parameter refers to the int value that you wish to convert to a string.
- buffer – the buffer parameter is a pointer to a char data type to hold the converted string.
- base – refers to the conversion base.
The function then returns a string of the specified integer.
The code below illustrates how to use the itoa() function to convert an int to a string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int var = 100 ;
char int_string [ 20 ] ;
Here, we specify the conversion of the int to base 10.
Closing
In this article, we covered two main methods of converting an Int to a string in C. It is good to stick to the Sprintf function as it’s a standard function and can be exported across systems.
Thanks for reading, and Happy coding!!
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list
Convert Int to String in C#
Converting an int to a string is a common task in C# programming. This process is useful when you need to display an integer value as text, concatenate integer values with other strings, or store integer values in a text file. In this blog post, we’ll explore the different ways to convert an int to a string in C#.
Convert Int to String in C# Using ToString() Method
The simplest way to convert an int to a string in C# is by using the ToString() method. This method is available on all int variables and converts the int value to a string.
Convert an Integer to a String in C
This tutorial introduces how to convert an integer to a string value in C. There are different methods for converting an integer to a string in C, like the sprintf() and itoa() functions.
Please enable JavaScript
sprintf() Function to Convert an Integer to a String in C
As its name suggests, it prints any value into a string. This function gives an easy way to convert an integer to a string. It works the same as the printf() function, but it does not print a value directly on the console but returns a formatted string. The return value is usually discarded, but if an error occurs during the operation, it returns -1 .
sprintf() Syntax:
- str is a pointer to a char data type.
- format is is used to display the type of output along with the placeholder.
- arg1 , arg2 are integers to convert into a string.
Example Code of sprintf() to Convert an Integer to a String in C
itoa() Function to Convert an Integer to a String in C
itoa() is a type casting function in C. This function converts an integer to a null-terminated string. It can also convert a negative number.