I was trying to understand the sprintf function and got this term, Could anyone please explain the term "formatted string" in a very simple language?
Asked
Active
Viewed 49 times
0
-
it means a string, which has been formatted - meaning: put into a particular format. Like the format "Hello, my name is
" – user253751 Jun 02 '22 at 16:56 -
@user253751 Sorry??Can you please demonstrate it by c++ code? – PHANTOM DRAGON Jun 02 '22 at 16:57
-
1It means, that variable values will be rendered in the format template string, and result in a formatted string. `sprintf()` is a c language function btw., and there are better ways to achieve that in c++. – πάντα ῥεῖ Jun 02 '22 at 16:57
-
This seems like a nice question. I don't see any reason for downvotes. – Anoop Rana Jun 02 '22 at 16:59
-
@AnoopRana no, it missed specific code and context. – πάντα ῥεῖ Jun 02 '22 at 17:01
-
@πάνταῥεῖ Actually,Could you please demonstrate it by c++ code – PHANTOM DRAGON Jun 02 '22 at 17:01
-
1@PHANTOMDRAGON Hello there. You can refer to any of the [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In the above linked list of books there are also some beginner level books that explain things in great detail. – Anoop Rana Jun 02 '22 at 17:03
-
@PHANTOMDRAGON I have no idea, what you're actually after. But abusing multiple Accounts, and undergoing question bans has been noticed. Don't do that!! – πάντα ῥεῖ Jun 02 '22 at 17:05
-
2There is no such term in the C or C++ language vocabulary, however `printf` and `scanf` function families (called *formatted input/output functions*) use something called *format strings*. By the way, if you are trying to learn C++, these functions should be one of the last things you learn. – n. 1.8e9-where's-my-share m. Jun 02 '22 at 17:06
-
find examples of usage of `sprintf` you'll understand what the "formatted string" refer to. – 440 Jun 02 '22 at 17:06
-
@n.1.8e9-where's-my-sharem. Defination of sprintf- The sprintf() function in C++ is used to write a formatted string to character string buffer. Now,What is formatted string,And I'm leaning sprintf function because – PHANTOM DRAGON Jun 02 '22 at 17:07
-
Because sprintf function could help to find the length of the int. – PHANTOM DRAGON Jun 02 '22 at 17:09
-
@PHANTOMDRAGON pass a `NULL` pointer for the buffer, to just get the necessary length. – πάντα ῥεῖ Jun 02 '22 at 17:12
-
1@PHANTOMDRAGON who said that? There are many far more efficient ways to find the length of an int: [Efficient way to determine number of digits in an integer](https://stackoverflow.com/q/1489830/995714), [how to find the length of an integer](https://stackoverflow.com/q/22648978/995714) – phuclv Jun 02 '22 at 17:13
-
@PHANTOMDRAGON but a more serious thing about your action here is to see that you're actively trying to undergo this sites policies. I've flagged that for mod attention! – πάντα ῥεῖ Jun 02 '22 at 17:14
-
@πάνταῥεῖ I didn't abused,Nor my any of account get banned,I was just banned for asking questions for maybe one day ,But i urgently want to ask a question and hence changed my account. – PHANTOM DRAGON Jun 02 '22 at 17:19
-
This quote is not from the C++ standard. Maybe some kind of popular book? In fact the C++ standard does not discuss `sprintf` at all. It is a function inherited from C. A "formatted string" is not a C or C++ term. It is just a string that is created by some kind of of formatting. Formatting is what all functions from the `printf` family do. Specifically `sprintf` puts the result in a string, hence "formatted string". "Because sprintf function could help to find the length of the int" now this just doesn't make a whole lot of sense. – n. 1.8e9-where's-my-share m. Jun 02 '22 at 17:19
1 Answers
1
A formatted string is simply a string created using some sort of formatting rules or some kind of template. There are many functions in C++ that you can use for this. The std::printf family of functions inherited from C is one example. One of the newer is std::format which is included in C++20:
Example:
#include <format>
#include <iostream>
#include <string>
int main(int argc, char* argv[]) {
// check if the user has supplied an argument:
if(argc != 2) {
std::cout << "Please supply your name!\n";
return 1;
}
// create a string from the provided template:
std::string formatted_string =
std::format("Hello {}, you are now in a formatted string!\n", argv[1]);
// print the result:
std::cout << formatted_string;
}
Ted Lyngmo
- 60,763
- 5
- 37
- 77