# printf used in c++ and the %

## 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.

##### Share on other sites
Quote:
 Original post by alvaroHowever, it's a pain to do formatted output with streams, since the manipulators in 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.

Of course they have both goods and bads, but is so damn easy to get printf wrong and introduce undefined behaviour that the compiler won't give any warning about.

C++0x will allow for typesafe format strings, btw.

##### Share on other sites
Quote:
 Original post by phresnelOf course they have both goods and bads, but is so damn easy to get printf wrong and introduce undefined behaviour that the compiler won't give any warning about.

g++ does offer useful warnings for common cases.

Quote:
 C++0x will allow for typesafe format strings, btw.

That's good news.

##### Share on other sites
Quote:
Original post by alvaro
Quote:
 Original post by phresnelOf course they have both goods and bads, but is so damn easy to get printf wrong and introduce undefined behaviour that the compiler won't give any warning about.

g++ does offer useful warnings for common cases.

Indeed, and if you happen to write C libraries, you can invoke that black magick for your own functions, too, via some attribute.

Quote:
Quote:
 C++0x will allow for typesafe format strings, btw.

That's good news.

Not shipped by standard, unfortunately. But by using user-defined literals and variadic templates, one can hack up something :)

(I tried out variadic templates with a formatting example, though user defined literals are not supported yet by g++, so it is not completely compile time save: plug)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628316
• Total Posts
2982033

• 9
• 9
• 13
• 11
• 14