stdio
is for standard IO in C. It should have a .h
at the end. In C++, all C headers have been encapsulated in cxxxxxx
headers (without .h
). So, <stdio.h>
is the same as <cstudio>
. These offer functions, like printf
and scanf
, for simple IO.
iostream
on the other hand is an IO library for C++, and offers streams like cin
and cout
, as you mentioned.
Depending on your application you can use them interchangeably for most of the time. The syntax is going to be different, obviously.
Formatting text can be easier using the C functions. For example:
printf("item %04d has a value of %+.6e\n", index, value);
is easier to write than (needs <iomanip>
in addition to <iostream>
):
std::cout << "item " << std::setw(4) << std::setfill('0') << index
<< "has a value of " << std::setprecision(6) << value << "\n";
However, you need to be more careful when using the first one. For example, the following line won't produce a compile error (but as sharth mentioned, you might get warnings when compiling) but will cause runtime issues:
printf("I wonder what will happen? %d\n");
I don't think there is a lot of difference in their performance as most of the stream "magic" happens in compile time, and they should produce similar results. I'm not 100% sure though, so correct me if I'm wrong.