C/C++: cout/ostream/istream vs (f)printf/scanf

  • C/C++
  • Thread starter neurocomp2003
  • Start date
In summary: Really, they are functionally the same. cout << setprecision(5) << value << endl; is much clearer than printf("%5f", value) in fact, I suspect I have the printf version wrong; I have to hit the man pages any time I want to do something nontrivial with C-style i/o. At one time, it used to take quite a while to find the relevant entry, but I've had to do it often enough that I can usually remember at where and in what man page I need to look.lol man pages...i hate linux/unix hehe...nah its %#.5f and I'm rather accustomed to using it especially doing things like %+-10
  • #1
neurocomp2003
1,366
4
is there any advantages to using one text I/O method over another
I'm more accustom to the prinft/scanf but i seem to see ostream/istream within C++ coding style( prolly because of the OO)

but is there really a difference?
 
Technology news on Phys.org
  • #2
Functionally they aren't very different. cout ,ostream, istream are just geared more toward OO programming like you mentioned.
 
  • #3
The main difference between the two is in the flexiblity which the iostream methods give you over the C standard functions. ostream is more extensible, allowing you to easily handle (overload) your user defined types while also ensuring type safety. Basically, you can do more complex things more elegantly with ostream.
 
  • #4
Sir_Deenicus, sure you can do all that, but for most people they don't use those capabilities so functionally to them it is basically the same.
 
  • #5
you mean i can't overload the printf with a printf(Type& ) operator and use varg in the function?
 
  • #6
Yeah, you can do that, but you can also overload << and >>.
 
  • #7
yeah i know you can overload the <<, >>

was just curious at what Sir_Deenicus was indicating about the
extensibility...i just don't like <<,>> because i find it easier to format text
with printf. rather than setprecession(...) etc.
 
  • #8
you mean i can't overload the printf with a printf(Type& ) operator and use varg in the function?
Not really. I assert it would be very difficult to create useful overloads printf so that it usually behaves similarly to that of the C library. The problem is that you'll have to overload it for every type combination that you'll ever expect to write.

Now, some libraries do provide mechanisms for introducing new types into printf's mechanism, and this should be doable. (But, of course, library specific)


One advantage that you should not ignore is this:

cout << setprecision(5) << value << endl

is much clearer than

printf("%5f", value)

in fact, I suspect I have the printf version wrong; I have to hit the man pages any time I want to do something nontrivial with C-style i/o. At one time, it used to take quite a while to find the relevant entry, but I've had to do it often enough that I can usually remember at where and in what man page I need to look.
 
  • #9
lol man pages...i hate linux/unix hehe...nah its %#.5f and I'm rather accustomed to using it especially doing things like %+-10.5f, +-10.5f I'm pretty sure you could probably right it into a string and send it into printf though, but I've seen some nice techniques if you know how to use varg vbegin.
 
  • #10
Well I had in mind where you hand to handle complex file loading and had to use such things as vector templates and probably templates in general and have them interact seamlessly with your objects. There, the ostream library is superior. Also I do not see why you would overload the printf -which you cannot do as an operator anyway- that is not a very good idea at all and is in principle not necessary. Also printf does not protect agaisnt buffer overflows and does not give you much safety or flexibility over strings...The list goes on.

Printf is just a quick and dirty way to get stuff out into output or vice versa with scanf.
 

FAQ: C/C++: cout/ostream/istream vs (f)printf/scanf

1. What is the difference between cout/ostream/istream and (f)printf/scanf in C/C++?

Cout/ostream/istream and (f)printf/scanf are two different ways of outputting and inputting data in C/C++. Cout/ostream/istream are part of the standard C++ library, while (f)printf/scanf are part of the standard C library. Cout/ostream/istream are more commonly used in C++ programs, while (f)printf/scanf are more commonly used in C programs.

2. Which one is more efficient, cout/ostream/istream or (f)printf/scanf?

In terms of efficiency, (f)printf/scanf may have a slight edge over cout/ostream/istream. This is because (f)printf/scanf are more low-level functions and allow for more control over the output and input process. However, the difference in efficiency is usually negligible and depends on the specific implementation.

3. Can I mix cout/ostream/istream and (f)printf/scanf in the same C++ program?

Yes, it is possible to mix cout/ostream/istream and (f)printf/scanf in the same C++ program. However, it is generally not recommended as it can lead to confusion and potential errors. It is better to stick to one method for outputting and inputting data in a program.

4. Are there any advantages of using cout/ostream/istream over (f)printf/scanf?

One advantage of using cout/ostream/istream is that it is more user-friendly and easier to use for beginners. In addition, cout/ostream/istream provide more type safety and error checking compared to (f)printf/scanf. This can help prevent common mistakes and improve the overall quality of the code.

5. Which one should I use, cout/ostream/istream or (f)printf/scanf?

The choice between cout/ostream/istream and (f)printf/scanf ultimately depends on personal preference and the specific needs of the program. If you are working on a C++ program, it is recommended to use cout/ostream/istream. However, if you are working on a C program, (f)printf/scanf may be a better choice. It is also important to consider the conventions of the team or organization you are working with.

Similar threads

Replies
40
Views
3K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
21
Views
9K
Back
Top