cout vs printf ???
Members - Reputation: 267
Posted 12 April 2007 - 09:11 PM
Members - Reputation: 202
Posted 12 April 2007 - 09:30 PM
Members - Reputation: 1134
Posted 12 April 2007 - 09:30 PM
You should of course know the syntax of both, but use cout whenever possible. Also the header is called cstdio in C++, not stdio.h as it's in C.
Members - Reputation: 1092
Posted 12 April 2007 - 11:44 PM
Members - Reputation: 1556
Posted 13 April 2007 - 12:08 AM
Original post by BUnzaga
My question is which is more standard?
Both are part of the C++ Standard Library (where printf is found under it's new header <cstdio>).
Are they both used in almost every application, or is one used and another not, or how do you decide which one you want to use? Which is better for gaming? Which should I memorize?
Performance is a non issue for comparison (the actual drawing of the console dwarfs the cost of using either method to send information to the console) -- for the most part, their functionality overlaps, but a few key points which apply to any programs using them:
1) Iostreams use operator chaining instead of va_args -- this means your program can't crash because you passed the wrong number of arguments, as can happen with printf.
2) Iostreams use operator overloading instead of va_args -- this means your program can't crash because you passed an int and it was expecting a string, as can happen with printf.
3) Iostreams don't have native support for format strings (which is the major root cause of 1 and 2). This is generally a good thing, but sometimes they're useful. The boost format library brings this functionality to iostreams for those who need it, with defined behavior (throws an exception) rather than undefined (spewing out bits of memory, or goodness knows what else) behavior (as is the case with printf). That said, this currently falls outside the standard (although many parts of boost are being incorperated into the upcomming next version of C++)
4) Iostreams, unlike their snprintf equivilants, can handle variable length buffers directly themselves, instead of you the programmer being forced to deal with hardcoded cruft or extranious loops.
So yeah, I'd go with iostreams. Even though I learned about them only after, in effect, mastering printf, I've since let my knowledge of printf's specifiers fade for the most part, having not used them myself for years (although I still recall major type specifiers).
Should I just remember the syntax for both, and just include both in all of my programs?
Well, understanding the syntax of printf will at least help you understand other people's code -- but I wouldn't write new code using it. Let me put it this way: there's
Members - Reputation: 690
Posted 13 April 2007 - 12:32 AM
imo, there are only two reasons to use printf():
-If you are working with legacy C code, or
-You are working with a system without the C++ runtime library
If you are using C++, I recommend using cout for the many reasons listed,
and only use printf() if you have to.
Crossbones+ - Reputation: 5504
Posted 13 April 2007 - 03:05 AM
Original post by MaulingMonkey
Well, understanding the syntax of printf will at least help you understand other people's code -- but I wouldn't write new code using it. Let me put it this way: there [are] many reasons an alternative method was introduced to C++'s standard library.... I also treat any reference on C++ that uses printf with extreme suspicion -- it hints of oudated, ancient information which tends to be at best misleading, and all-too-often, downright wrong.
Quoted for emphasis.
Moderators - Reputation: 3892
Posted 13 April 2007 - 03:19 AM
Since, as you note, std::cout is an instance of std::ostream, it is possible for you to perform your output on an instance of std::ostream (or rather, a reference to one), instead of on std::cout directly
int DoSomething(std::ostream &outputSink)
outputSink << "Doing something complex..." << std::endl;
outputSink << "Okay, done." << std::endl;
which is advantageous:
You leave the determination of whether or not to output to stdout, stderr, or a file (in short, the actual concrete destination of the output) to the user of the function, who can presumably make a better-informed decision about that output destination than the function itself, especially if the IO is just an informative side-effect of the function (as in this case; it's just log output, the real meat of the function is the computation of the meaning of the universe).
This also means you could even provide a std::ostream instance (or subclass) with a no-op backing buffer, to suppress output altogether (although there are more ideal solutions to this problem, as it doesn't prevent the function from doing any of the overhead work to generate and format the output, but this may not be a concern).
Members - Reputation: 961
Posted 13 April 2007 - 03:20 AM
Original post by swiftcoderQuote:
Original post by ToohrVyk
Use an std::ostream, not std::cout.
cout is an ostream, so I fail to see the point of this comment?
I'm guessing ToohrVyk is pointing out that if you have (for example) a class which takes a std::ostream as a parameter to its constructor, and then write to that rather than std::cout, then your code is that bit more flexible - output can be routed to a file, say, for a 'quiet mode'.
Members - Reputation: 1372
Posted 13 April 2007 - 08:09 AM
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Moderators - Reputation: 3892
Posted 13 April 2007 - 08:29 AM
Correct me if I'm wrong, but you can redirect cout to point to something else to achieve exactly the same effect with less need to pass around parameters?
Depends how you mean. You can redirect from the shell, but that's completely out of the program's control. You can replace the streambuf in code, but that's less intuitive than replacing what you write to, I feel (less obvious), and its clunky if you're going to need multiple redirects. But yes, its an option.
Members - Reputation: 1591
Posted 13 April 2007 - 08:56 AM
Original post by jpetrie
I'm no mind reader, but I suspect the comment has to do with generality.
You have explained my thoughts very well. I was indeed hinting at the possibility to use your ostream-based code with any ostream, and not just std::cout.
If not for generality, std::cout does not offer a lot over printf: type-safety is commonly handled by most compilers for printf as well (detecting type mismatches between format string and provided arguments), and I find the syntax for printf to be much more elegant than the one for streams (which is why I'm so happy about OCaml using printf instead of C++ formatting). The only downside to printf in C++ (aside from generality issues) is that it's not idiomatic.
Members - Reputation: 1020
Members - Reputation: 267
Posted 13 April 2007 - 08:15 PM
In my experience as a brand BRAND new C/C++ student, I was browsing around on the net looking for tutorials on C/C++. I would read through one and it would tell me to do my basic 'Hello World' program and everything was great, until I read a different tutorial and it had some totally different way to do it.
So I didn't know which way was 'right' or if the tutorials I was using were valid, or what the heck was going on. Finally I came accross the Zeus CMD tutorils which explained that printf went with stdio.h, which was for C, and that cout went with iostream, which was for C++.
In the Zeus tutorial he had an excersize showing how each was used for formatting, etc etc, and we included both header files, so I just started thinking what was the most common or industry standard way to use these two headers, and in which situation I would choose one over the other, or if I should just stick with one and disregard the other.
My end goal is to land a job as a game programmer, and eventually get into 3d programming and AI. I would also like to produce my own series of all kids of games ranging from simple single player puzzle or tetris games to the full scale multi-player games using client/server networking, databases, and all that other stuff that I have no idea about.
SO. With that information, what should I do?
Banned - Reputation: 108
Posted 13 April 2007 - 08:25 PM
A programmer who is well versed in C and C++ will use C-style IO.
printf is much more compact.
try printing out some number in showpoint, precision 2, non-scientific-notation, width 5.
in cout you'll have to throw something into the stream for each modifier, in printf it's just a compact 3 or 4 characters preceeding the variable.