
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Easiest Way to Convert int to String in C++
In this section, we will see how to convert an integer to a string.
The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.
Input: User will put some numeric value say 42 Output: This program will return the string equivalent result of that number like “42”
Algorithm
Step 1: Take a number from the user Step 2: Create an empty string buffer to store result Step 3: Use sprintf() to convert number to string Step 4: End
Example Code
#include<stdio.h> main() { char str[20]; //create an empty string to store number int number; printf("Enter a number: "); scanf("%d", &number); sprintf(str, "%d", number);//make the number into string using sprintf function printf("You have entered: %s", str); }
Output
Enter a number: 46 You have entered: 46
Advertisements