Sign in to follow this  
MikeWhiskyTango

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


Link to post
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 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
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
Quote:
Original post by alvaro
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.


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


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

g++ does offer useful warnings for common cases.

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

That's good news.

Share this post


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

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)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this