954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

noob question

I have just started doing a little C programming and, looking here and there, I have noticed a lot of people prefers to use library (and related commands "cout" and "cin") instead of using library (and related commands "printf" and "scanf")

What are the advantages and/or disadvantages (if any) of doing so?

Thanks to anybody who will try to answer my question :cheesy:

Fanion
Newbie Poster
2 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
I have just started doing a little C programming and, looking here and there, I have noticed a lot of people prefers to use library (and related commands "cout" and "cin") instead of using library (and related commands "printf" and "scanf")

cin and cout are C++, not C, and the proper header should be . They can be safer to use than printf and scanf because you don't have to match format specifiers to the data.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Fanion, you seem to be confused about what language you're learning. is not C. In fact, it's no longer valid C++. However, ignoring the fact that you have no idea what your question is, I'll give you a few reasons for using iostreams instead of C-style I/O in C++:

1) Safer.

You no longer have to worry about matching the type of a value with a format modifier such as with the printf or scanf families. Formatted I/O using iostreams will figure out the type for you, thus removing a category or errors common to C.

2) Extensible.

You're not restricted to only the types that the standard says you can use. With printf, for example, you can't pass a user defined object and expect any meaningful output. Using iostreams, you can overload the << operator for your type and cout will print it correctly, however you want.

3) Consistent.

There are annoyingly subtle differences between the format strings of scanf and printf. A lot of people fall into the trap of using %f with scanf when they really mean %lf, because printf doesn't make that distinction. On the other hand, iostreams are very consistent in how they work, and you can generally move from input to output without an unintuitive change in syntax or semantics.

4) Flexible and Powerful.

Combining all of the advantages makes iostreams far more flexible and powerful than C-style I/O. The deeper you go, the more powerful they get.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

So, after all, cin and cout are standard C++ instructions, while printf and scanf are C instructions...hmm...this makes sense ;)

This also explains why I've been taught to use printf and scanf (after all I am studying C, not C++...even if I still don't get completely the difference between these two languages ..... DOH :o )

Thanks everybody for your help!!

Fanion
Newbie Poster
2 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>cin and cout are standard C++ instructions, while printf and scanf are C instructions
Not really. cin and cout are standard C++ objects, while printf and scanf are standard functions available in both C and C++, but they are less useful in C++ than cin and cout, so you only see them used in old code or by exceptionally stubborn programmers.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You