noob question

Reply

Join Date: Jul 2005
Posts: 2
Reputation: Fanion is an unknown quantity at this point 
Solved Threads: 0
Fanion Fanion is offline Offline
Newbie Poster

noob question

 
0
  #1
Jul 11th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: noob question

 
0
  #2
Jul 11th, 2005
Originally Posted by Fanion
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: noob question

 
0
  #3
Jul 11th, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: Fanion is an unknown quantity at this point 
Solved Threads: 0
Fanion Fanion is offline Offline
Newbie Poster

Re: noob question

 
0
  #4
Jul 11th, 2005
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!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: noob question

 
0
  #5
Jul 11th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC