
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the C++ equivalent of sprintf?
The sprint() function is present inside the C and C++ also. This function is used to store something inside a string. The syntax is like the printf() function, the only difference is, we have to specify the string into it.
In C++ also, we can do the same by using ostringstream. This ostringstream is basically the output string stream. This is present in the sstrem header file. Let us see how to use this.
Example
#include<iostream> #include<sstream> using namespace std; int main() { string my_str; ostringstream os; os << "This is a string. We will store " << 50 << " in it. We can store " << 52.32 << " also."; my_str = os.str(); //now convert stream to my_str string cout << my_str; }
Output
This is a string. We will store 50 in it. We can store 52.32 also.
- Related Articles
- What is the use of sprintf() and sscanf() functions in C language?
- What is Python equivalent of the ! operator?
- What is the equivalent of EXCEPT in MySQL?
- What is the Certainty Equivalent Method?
- sprintf() function in PHP
- What is equivalent ratio?
- What is the equivalent of SQL “like” in MongoDB?
- What is the equivalent of C# namespace in Java?
- What is the C# equivalent of C++ friend keyword?
- What is the C# Equivalent of SQL Server DataTypes?
- What is the MySQL SELECT INTO Equivalent?
- What is the PHP equivalent of MySQL's UNHEX()?
- What is the equivalent of a VB module in C#?
- What is the equivalent of Java static methods in Kotlin?
- What is the PHP stripos() equivalent in MySQL?

Advertisements