cout vs printf ???

Started by
22 comments, last by MaulingMonkey 17 years ago
Quote:Original post by BUnzaga
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.


C, C++, and the (commercial and/or available to the public) Internet have all been around for a long time. A lot of information out there is out of date. Worse yet, many people don't bother to keep current, and MANY people think they know a lot more about C and C++ than they really do ;), and write tutorials that hold everyone else back :(

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


Well, first of all expect it to take a long time to reach the end of your road, just like it would to reach the end of any other road of the same sort.

And then, start learning. And then make sure you know the basics.
Advertisement
Quote:Original post by sharpnova
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.


No, s/he wouldn't, necessarily. I can tell you that because I know from forum experience that both MaulingMonkey and myself are "well versed" in both languages, and I can be pretty confident Bregma is too, based on, well, his job - and I think I can safely speak for the three of us when I say none of us would readily go back to stdio when there are better options available.

Quote:printf is much more compact.

try printing out some number in showpoint, precision 2, non-scientific-notation, width 5.


I assume you mean a floating-point value. Is it a float or a double or a long double? C++ doesn't care. (boost::format doesn't either).

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


Well, not really "preceding the variable"; it's stuff that's mixed in to a format string, while all the variables occur after. And, you know, each of those characters has some meaning, and is something you have to "throw in" separately, and think about. Compactness doesn't always translate into simplicity.

But when you do have a valid reason for embedding formatting information into a base string and then filling out that "template" (i18n/l10n comes to mind), there is boost::format.
Quote:Original post by sharpnova
A programmer who is well versed in C and C++ will use C-style IO.

No! A well-versed programmer in C++ knows Boost and therefore the choice will always be between iostreams and boost::format. You don't even get the option to print user-defined types using printf.

Quote:printf is much more compact.

Like boost::format which is also:
- type-safe
- efficient
- intuitive

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

And if you happen to make one slight mistake your whole program could crash without warning. If that was a good reason to use printf then we should all go back to use one-letter identifiers. We now have tools like auto-completion in our IDE and typing a couple of extra characters have never hurt anyone.

A beginner (as in at least a couple of months) writing code can easily chunk out 500 lines of code/day, but in a professional environment where quality of code and debugging is high-priority it's (at least in my experience) uncommon to write more than 100 lines of code/day (usually less, depending on the project). Also all your IO should be pretty isolated from other code anyway so how many calls to IO constructs do you actually make?

There are well-known problems with iostreams (most notably the lack of format/content seperation), but there are thousands of well-known very serious problems with printf.
Sorry to necroishpost, but...

Quote:Original post by sharpnova
A programmer who is well versed in C and C++ will use C-style IO.


I used to think this. Then I actually became well versed in C++, and stopped taking for granted printf's numerous, fundamental problems. Especially when I tried teaching some of my friends how to program.

The moment the point finally sunk in completely?

Our yearbook came back, where everyone had written stuff. One of them contained a corney rerendition of "Hello World". I think it was "Hey Mike" or something.

The printf format specifier was wrong.

I hung my head in shame and resolved never to inflict printf upon anyone ever again.

This topic is closed to new replies.

Advertisement