Jump to content

  • Log In with Google      Sign In   
  • Create Account

cout vs printf ???


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
23 replies to this topic

#1 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 12 April 2007 - 09:11 PM

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

Sponsor:

#2 DevFred   Members   -  Reputation: 836

Like
0Likes
Like

Posted 12 April 2007 - 09:22 PM

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.


#3 thallish   Members   -  Reputation: 202

Like
0Likes
Like

Posted 12 April 2007 - 09:30 PM

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

#4 CTar   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 12 April 2007 - 09:30 PM

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.

#5 ToohrVyk   Members   -  Reputation: 1591

Like
0Likes
Like

Posted 12 April 2007 - 09:33 PM

Use an std::ostream, not std::cout.

#6 boldyman   Members   -  Reputation: 138

Like
0Likes
Like

Posted 12 April 2007 - 09:58 PM

In C++ you should always use cout. Because it is fast!!
Use the standard library where ever possible.


www.nextdawn.nl
www.moneyaside.com

#7 Captain P   Members   -  Reputation: 1092

Like
0Likes
Like

Posted 12 April 2007 - 11:44 PM

@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?
Create-ivity - a game development blog Mouseover for more information.

#8 MaulingMonkey   Members   -  Reputation: 1556

Like
0Likes
Like

Posted 13 April 2007 - 12:08 AM

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.

#9 Crypter   Members   -  Reputation: 690

Like
0Likes
Like

Posted 13 April 2007 - 12:32 AM

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.


#10 Bregma   Crossbones+   -  Reputation: 5504

Like
0Likes
Like

Posted 13 April 2007 - 03:05 AM

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

#11 swiftcoder   Senior Moderators   -  Reputation: 10448

Like
0Likes
Like

Posted 13 April 2007 - 03:09 AM

Quote:
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?

#12 Josh Petrie   Moderators   -  Reputation: 3892

Like
0Likes
Like

Posted 13 April 2007 - 03:19 AM

I'm no mind reader, but I suspect the comment has to do with generality.

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;
ComputeMeaningOfTheUniverse();
outputSink << "Okay, done." << std::endl;
return (42);
}


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

#13 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 13 April 2007 - 03:20 AM

Quote:
Original post by swiftcoder
Quote:
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'.

#14 SunTzu   Members   -  Reputation: 286

Like
0Likes
Like

Posted 13 April 2007 - 08:05 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?

#15 JohnBolton   Members   -  Reputation: 1372

Like
0Likes
Like

Posted 13 April 2007 - 08:09 AM

Well, it's not a slam-dunk. Formatting output using iostream is complicated and unwieldy. printf is a much better choice when you have to do a lot of formatting. However you can have the best of both worlds using boost::format.

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

#16 Josh Petrie   Moderators   -  Reputation: 3892

Like
0Likes
Like

Posted 13 April 2007 - 08:29 AM

Quote:

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.

#17 ToohrVyk   Members   -  Reputation: 1591

Like
0Likes
Like

Posted 13 April 2007 - 08:56 AM

Quote:
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.



#18 jflanglois   Members   -  Reputation: 1020

Like
0Likes
Like

Posted 13 April 2007 - 11:42 AM

The one thing that is too bad about standard C++ streams is that it is harder to separate content from presentation. Of course, I'm not recommending you use printf, but certainly take a look at boost::format for a type-safe equivalent.

#19 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 13 April 2007 - 08:15 PM

Hello and thank you for the informative responses! I was mainly trying to figure out when or why I would use one over the other, or if I should include both.

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?

#20 sharpnova   Banned   -  Reputation: 108

Like
0Likes
Like

Posted 13 April 2007 - 08:25 PM

You'll get a lot of different answers to this.

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS