| | |
noob question
![]() |
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
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 <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:
What are the advantages and/or disadvantages (if any) of doing so?
Thanks to anybody who will try to answer my question :cheesy:
•
•
•
•
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")
"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
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.
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.
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Solved Threads: 0
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!!
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.
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.
![]() |
Similar Threads
- Noob with a question (IT Professionals' Lounge)
- Question (Networking Hardware Configuration)
- Noob question about Defining and Declaring. (C++)
- NOOB question , graphical tiles (C++)
- What exploit are they using? (Web Browsers)
Other Threads in the C Forum
- Previous Thread: Help regarding multi-threading
- Next Thread: In Need of a Script
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






