stdio.h vs iostream

Generally I have come across "hello world" tutorials that use the iostream library, but when I was talking about it with my brother who is a C++ programmer, he was not familiar with it. He told me that he uses stdio.h, or I think he referred to it as standard input/output.

So I did some looking around and found a "hello world" tutorial using stdio.h and was wondering which would be better for me to practice. Are there any benefits to using one or the other?

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
  cout << "Hello World!";
  return 0;
}

1
2
3
4
5
6
7
#include <stdio.h>

int main()
{
  printf("Hello World!");
  return 0;
}
The Standard C runtime library uses stdio.
The Standard C++ runtime library uses iostream.

Because C++ is largely a superset of C, it includes the C stanard library, so stdio based code works.

The recomended way of using stdio in C++ is to use a slightly different header file.
1
2
3
4
5
6
7
#include <cstdio>

int main()
{
  printf("Hello World!");
  return 0;
}
Your brother cannot both be a C++ programmer and have no idea what the iostream library is.
Yeah, seriously. iostream is almost as old as C++, so either he learnt C++ during its early development stages in the 80s, or he thinks C and C++ are essentially identical.

As for which is better, it doesn't matter much. It's just a Hello World. The one that uses iostream is the C++ way, though.
Last edited on
closed account (S6k9GNh0)
He probably thinks he codes in C++ but is actually a C-based programmer. Still, most C programmers at least know the basics of C++ so I dunno.

EDIT: Also, we didn't answer his question. He asked if there was a difference as in is one optimized over the other. I don't know the answer so I can't give one.
Last edited on
He didn't ask that. He asked, "which would be better for me to practice. Are there any benefits to using one or the other?"

The answer is, "When in Rome, do as the Romans do." If you are using C++, use the iostream library.
http://www.google.com/search?q=iostream+vs+stdio

Hope this helps.
Sorry, maybe my brother is a C programmer. I am a newbie programmer so I am still learning.

So, when it comes to cstdio and iostream, it's just a matter of preference? They are both used for the same thing? Is one more cross-platform than the other?
I suppose you could call it a matter of preference. Yes, they are used for essentially the same thing, and they are equally cross-platform. However, iostream trumps stdio in a lot of ways. Read the links I gave you.

Good luck!
it's just a matter of preference?
Sort of. Neither can do more than the other (at least not as far as I know), but like I said before, iostream is the C++ way. C++ classes, however, do provide more memory-safety mechanisms than C, and that's always a plus.
Thank you everyone, I understand much better now. I'm going to continue to practice with iostream but will definitively learn how to use cstdio as well.
Topic archived. No new replies allowed.