• 13
• 27
• 9
• 9
• 20

# printf used in c++ and the %

This topic is 2713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am self learning C++ and keep seeing printf.
Its not mentioned in any of the tutorials. When I googled it most sites state
its the same as cout. Yet its syntax is different, C syntax, I believe, but whats with the modulus I often see in the syntax ie; below. That syntax I sometimes see in c++ code that use printf??
printf("%d, %d",

##### Share on other sites
When in doubt, read the manual.

##### Share on other sites
The modulus in a printf statement indicates a placeholder for a variable, and the letter immediately following indicates the type of variable to fill it with. %d indicates an integer, %s a string and so on.

int n = 99;
string s = "Bottles_of_beer";
printf("%d %s", n, s);

prints
99 Bottles_of_beer. Adding /n inside the quotes is the newline character.

##### Share on other sites
Thanks.

It was the distinctly user unfriendly reference part of that manual that gave me the doubts!

##### Share on other sites
Quote:
 Original post by PrintFDebuggerThe modulus in a printf statement indicates a placeholder for a variable, and the letter immediately following indicates the type of variable to fill it with. %d indicates an integer, %s a string and so on.int n = 99;string s = "Bottles_of_beer";printf("%d %s", n, s);prints99 Bottles_of_beer. Adding /n inside the quotes is the newline character.

That code will produce an error and crash, since %s represents an array of chars (C style string), not a C++ string.

For it to work, you have to do one of the following:

int n = 99;string s = "Bottles_of_beer";printf("%d %s", n, s.c_str());

int n = 99;char s[] = "Bottles_of_beer";printf("%d %s", n, s);

Thanks.

##### Share on other sites
Take LennyLens reply as a lesson for why you should avoid printf in C++, and use iostreams instead, which are typesafe and which can also be used upon std::string (and for advanced users: it can also be extended to support your own types).

##### Share on other sites
Also, you can add conditionals to a cout stream which can be handy

int num_contestants = count_contestants();cout << num_contestants << " contestant" << (num_contestants==1? "":"s") << endl;

##### Share on other sites
Quote:
 Original post by AngleWyrmAlso, you can add conditionals to a cout stream which can be handy*** Source Snippet Removed ***

That is not exclusive to iostreams, though.

##### Share on other sites
However, it's a pain to do formatted output with streams, since the manipulators in <iomanip> are just insane. I can never remember their names (setprecision to set the precision, but setw to set the width) or whether they only apply to the next item or to all of them, or how to restore the settings when I am done. And why setw only applies to one field but setfill applies to all just makes no sense at all.

Also, printf does things that streams cannot, like displaying the variables in any order, just changing the formatting string.

Perhaps Boost Format is the way to go.