I have just started doing a little C programming and, looking here and there, I have noticed a lot of people prefers to use <iostream.h> library (and related commands "cout" and "cin") instead of using <stdio.h> 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:

Recommended Answers

All 4 Replies

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

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

Fanion, you seem to be confused about what language you're learning. <iostream.h> 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.

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!!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.