2

I tried googling this but I get different answers at different places. I want to know the cases wherein one should use one of the following:

#include <stdio>
#include <cstdio>
#include <iostream>

I cannot figure out the difference since in my case all my C++ programs seem to work if I use these interchangeably. That being said, iostream seems to support the stream of input and output by defining cin and cout etc. However, I maybe wrong. I would appreciate answers / credible citations for the usage of these with reference to the C++ standards. I wonder if there are any performance benefits involved in using one over the other.

EdChum
  • 294,303
  • 173
  • 671
  • 486
ibp73
  • 632
  • 6
  • 14

3 Answers3

1

Nonstandard Headers

  • <stdio> is not defined in any of the standards that I know of.

Standardized Headers for C

  • <stdio.h> is the c header containing functions like printf() and scanf().

Standardized Headers for C++

  • <stdio.h> is included in the c++ standard but is deprecated.
  • <cstdio> is the c++ header that includes things like printf() and scanf().
  • <iostream> is a c++ header that include things like std::cout, std::cerr and std::cin.
Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
1

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.

triple_r
  • 972
  • 1
  • 7
  • 21
  • 1
    The last statement shouldn't produce a compiler error, but many compilers do recognize `printf()` and will produce a warning. – Bill Lynch Sep 03 '14 at 14:10
  • only the `sprintf` and similar variants are unsafe, because you can overflow the write buffer. Reading/writing in files/standard input/output is not unsafe. Using the wrong number of parameters in a function call is not a safety issue, rather a programming error – pqnet Sep 03 '14 at 14:11
  • remember that you're also using in your post, should be noted somewhere – BeyelerStudios Sep 03 '14 at 14:16
  • @sharth haven't used printf recently, so didn't know about the warnings. Editted my answer to mention this. Thanks. – triple_r Sep 03 '14 at 14:27
  • @pqnet Thanks, didn't mean unsafe in that regard, but unsafe in that it doesn't produce errors if the format string is wrong. I should be more careful with my word choice :-) – triple_r Sep 03 '14 at 14:28
0

there is no stdio (stdio.h and cstdio). the 'c' and the missing '.h' in the header name indicates that it's the C++ version of the C header.

check cstdio and iostream (references)

some compilers (including MSVC) include stl headers in other stl headers which leads to the effect you observed. this is not portable though!

if you are concerned with performance: use the C++ variants and check this

BeyelerStudios
  • 4,145
  • 17
  • 35
  • you mean to use `cin`/`cout` instead of `printf`/`scanf`? Because from what I had observed a while ago this doesn't really perform better. – pqnet Sep 03 '14 at 14:09
  • 1
    it doesn't perform better, it performs as good. plus it's type safe, you can overload the output operator for any of your custom types, thus it provides much more room and comfort – BeyelerStudios Sep 03 '14 at 14:15
  • Streams are polymorphic, so whatever you put in there will be printed. Most compilers would warn you if you forget a `*` and write `printf("%d",myPointer);`, but streams will happily print an address (which is also harder to spot by a human reading the code: maybe you really wanted to print a pointer). [FSharp's `printf` function](http://msdn.microsoft.com/en-us/library/ee370560.aspx) is type safe. Sadly there is no equivalent in C++ (as of now, maybe this weekend I'll write a library) – pqnet Sep 03 '14 at 14:40
  • STL already implements a better, less error-prone, generic way of doing output: streams – BeyelerStudios Sep 03 '14 at 15:00