ibp73
ibp73

Reputation: 650

C++ I/O library

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.

Upvotes: 2

Views: 1108

Answers (3)

triple_r
triple_r

Reputation: 1047

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.

Upvotes: 1

Bill Lynch
Bill Lynch

Reputation: 82026

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.

Upvotes: 1

BeyelerStudios
BeyelerStudios

Reputation: 4283

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

Upvotes: 0

Related Questions