This question confuses me lot. Because C++ is a superset of C programmer is free to use C's library functions like printf(), scanf() & many others etc. But I usually like C++ 's Object oriented I/O system & I mostly like to use cout & cin. Because iostream is more type safe, less error prone, extensible, flexible & also inheritable. Should I stop using traditional C's I/O functions because of iostream 's advantages or should I modify my program to use ? Which approach is better? Where should I take care when mixing C & C++ I/O. I know that backward compatibility with legacy C programs is necessary, but what should I really do?
Asked
Active
Viewed 378 times
-4
-
1C++ is not a super-set of C. It's a different language that embed also the C. – Sir Jo Black Apr 17 '15 at 17:04
-
Try malloc and delete together and see how that works for you :) – Michael Dorgan Apr 17 '15 at 17:08
-
1By default go with the methods that let you express what you are doing most clearly. Typically C++ streams. – Johan Lundberg Apr 17 '15 at 17:09
-
@SergioFormiggini: If it embedded C, then it would be a superset. It's not a superset because it only "embeds" _most_ of C, but not all of it. – Mooing Duck Apr 17 '15 at 17:09
-
1@MichaelDorgan: No, there is no any guarantee that combination malloc() and delete operator will work. It's undefined behavior. – Destructor Apr 17 '15 at 17:09
-
@meet, correct. Read for example the short and good "A Tour of C++" get a good and pragmatic starting point on what styles to use. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Also have a look at the answer by Nibot here: http://stackoverflow.com/questions/1042110/using-scanf-in-c-programs-is-faster-than-using-cin?rq=1 – Johan Lundberg Apr 17 '15 at 17:13
-
Sorry guys. I was picking soemthing I knew was wrong as a counter example to try and prove a point. Instead, I look like an idiot. Nothing to see here... – Michael Dorgan Apr 17 '15 at 17:59
1 Answers
2
C++ and C streams are synchronized by default, so you can mix them safely. This behavior is controlled by std::ios_base::sync_with_stdio.
As to whether you should do it? Doesn't matter. C++ does not have a universal style guide. Some programmers prefer the C++ iostreams interface, some prefer C's methods, some mix them.
Here are some links that discuss the problem better than I can.