Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stdio vs iostream [closed]

When I search on the internet for the difference between these two libraries, everyone says that <iostream> is the standard I/O library of C++ and <cstdio> is for C. My professor says that cin>> and cout<< are not good functions and if we use cin>> many times our application will definitely crash. He also says that stdio provides nearly 3 times faster input and output than iostream. However, I prefer using iostream because it is more convenient, and also I don't know if my professor is right.

So what do you advise me to use?

like image 782
Sam379 Avatar asked Jun 25 '13 06:06

Sam379


People also ask

Is iostream better than stdio?

Consider stdio to be the foundation upon which iostream was built. It is ultimately up to the one writing the code as to which header file they would like to use, but iostream will most likely reduce errors, increase type safety, and provide inheritability.

Does stdio work in C++?

YES WE CAN USE BECAUSE INPUT AND OUTPUT OPERATIONS CAN ALSO BE PERFORMED IN C++ ALSO. BECAUSE C/C++ USES WHAT ARE CALLED STREAMS TO OPERATE WITH PHYSICAL DEVICES SUCH AS KEYWORDS,PRINTERS TERMINALS OR ANY OTHER TYPE OF FILES SUPPORTED BY THE SYSTEM.

Can I use iostream in C?

There is no analog to iostream in C -- it lacks objects and types. If you're using C++, it's the analog to <cstdio> . See also this fantastic question and its answer, 'printf' vs.

What is cout in Stdio H?

The cout object is used to display the output to the standard output device. It is defined in the iostream header file.


2 Answers

Use streams in C++ and stdio.h in C. Yes, streams are a bit slower, but do those milliseconds count? User input is rarely a bottleneck of an application.

And if streams are used properly, and your compiler/runtime libraries are ok, your application won't crash.

But, if you have good, explainable reasons to use cstdio functions, then it is fully legitimate to use them in C++ as well.

like image 59
Alex Shesterov Avatar answered Sep 24 '22 01:09

Alex Shesterov


Using iostream should not make your program crash. It can be slow, but that's only because it's trying to interoperate with stdio. That synchronization can be turned off1. iostream is the idiomatic C++ way to get input, and I'd recommend its use over stdio functions in most cases when using C++.

1 using std::ios::sync_with_stdio(false);

like image 40
icktoofay Avatar answered Sep 24 '22 01:09

icktoofay