I know that std::cout
is the C++ equivalent of printf
.
What is the C++ equivalent of sprintf
?
sprintf() in C sprintf stands for "string print". In C programming language, it is a file handling function that is used to send formatted output to the string. Instead of printing on console, sprintf() function stores the output on char buffer that is specified in sprintf.
The only difference between sprintf() and printf() is that sprintf() writes data into a character array, while printf() writes data to stdout, the standard output device.
The sscanf () function and the sprintf () function are like the two sides of a coin. You can now use the sprintf() function to reassemble the string. You can use the same char array stringa- its previous value gets overwritten. Try it out for yourself to get a better grasp on it.
The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character. The commonly used format specifiers in printf() function are: Format specifier.
std::ostringstream
Example:
#include <iostream> #include <sstream> // for ostringstream #include <string> int main() { std::string name = "nemo"; int age = 1000; std::ostringstream out; out << "name: " << name << ", age: " << age; std::cout << out.str() << '\n'; return 0; }
Output:
name: nemo, age: 1000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With