Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fractal

printf/cout, scanf/cin

This topic is 6111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What''s the difference between cout and printf, or cin and scanf? Could I be any more dumber? (What do you mean, "No"?)

Share this post


Link to post
Share on other sites
Advertisement
I _think_ that printf, and scanf are from the days of C, and cout, and cin came along with C++... the differences, though i''m not too sure.

check the msdn, might help.
Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."

Share this post


Link to post
Share on other sites
I can help a little CIN is a Console Input like sa you wanted a use to enter his/her age and COUT our prolly get Console Out puts a string out. and I think... Printf is a same thing as COUT.. not to sure about that one.

Share this post


Link to post
Share on other sites
cout and cin are stream objects, while printf and scanf are functions.

Jesus is knocking at the door of your heart, won''''t you let him in?

Share this post


Link to post
Share on other sites
One bit of info (don''t quote me on this though):

the cout/cin/cerr are more reliable

but printf/scanf functions are very slightly quicker

Mind you if your using these function I can''t believe that there will be any relevent difference.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by xstreme2000
One bit of info (don''t quote me on this though):

the cout/cin/cerr are more reliable

but printf/scanf functions are very slightly quicker

Mind you if your using these function I can''t believe that there will be any relevent difference.


Neither is more reliable than the other, when used correctly. I don''t think there''s much speed difference either.

They are just two different interfaces to the standard input/output streams. There''s also write() and read(), if you like lower level printing.

It doesn''t matter which one you use in the slightest. Just don''t mix them in one program.

Share this post


Link to post
Share on other sites
In most implementations the C i/o library is faster with large amounts of data. That doesn''t mean your compiler or all compilers are the same way, they each have to implement the C and C++ standard libraries on their own. Neither is more reliable than the other, but the C++ version is more type safe (sometimes to your detriment, but C++ has its ways of getting around those problems).

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
cin/cout are type safe. The type of the operand determines how IO is performed. With printf/scanf, you have to tell the function the type of its arguments seperately, so theres scope for errors like:

printf("%s", `a`);

Here, a single char is printed as a string, which is a bad thing because printf is expecting the address of a null terminated array of characters. Instead it gets a single char, which is probably a value below 127. The equivalent with cout can`t go wrong:

cout << `a`;

Thats the main advantage of cin/cout - they protect you from your own errors. The disadvantage is that the syntax is horrible if you want columns of data printed.

Share this post


Link to post
Share on other sites
quote:
Original post by Krunk
The disadvantage is that the syntax is horrible if you want columns of data printed.


#include <iomanip>
...
// print age in an 8-cell column
// use blanks for padding
cout << setw(8) << setfill('' '') << age << endl;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!