cout vs printf ???
Hello I am new to programming in C++, and I am doing various tutorials, and for some time I was really frustrated and confused that in some tutorials they would tell me to use iostream and cout and in other tutorials they would tell me to use stdio.h and use printf. I finally learned that the stdio.h was for use with C and iostream is used for C++.
My question is which is more standard? 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?
Should I just remember the syntax for both, and just include both in all of my programs?
Thanks,
BUnzaga
Quote:Original post by BUnzaga
Hello I am new to programming in C++ [...] My question is which is more standard?
If you're programming in C++ use cout.
Using cout would also be my recommendation, but more important be consistent with the use. Do not use both. IMO that just leads to cluttered code. It is no problem if you have learned both, more power to you, but with power comes responsibility[wink]. Well that was probably over the top, but you get the point.
Good Luck
Good Luck
cout should definitely be used. It's type-safe, easy to use, extendable, generic, etc. It sometimes surprise people that generally cout is actually faster than printf. There is absolutely no reason to use printf in a C++ program.
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.
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.
In C++ you should always use cout. Because it is fast!!
Use the standard library where ever possible.
www.nextdawn.nl
www.moneyaside.com
Use the standard library where ever possible.
www.nextdawn.nl
www.moneyaside.com
@ToohrVyk: I'm curious, why? Actually, now that I'm looking it up, it turns out that std::cout is an instance of std::ostream. I guess you're pointing at how global std::cout really is?
Quote: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>).
Quote: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).
Quote: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 a reason many reasons an alternative method was introduced to C++'s standard library (to the point where I refuse to use the methods inherited from C in that area on principle). 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 at all-too-often, downright wrong. Either that or ultraconservative programmers. Then again, that's because ultraconservative programmers tend to be misleading :P.
printf() is C, not C++. C++ only supports it for backward compatibility with C.
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.
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.
Quote: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.
--smw
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement