Jump to content
  • Advertisement
Sign in to follow this  
MikeWhiskyTango

printf used in c++ and the %

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by PrintFDebugger
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.

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);

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by AngleWyrm
Also, you can add conditionals to a cout stream which can be handy

*** Source Snippet Removed ***


That is not exclusive to iostreams, though.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!