iostream

when or why should i use iostream library!?
What do you think that it does?
it is a header file which is used to include standard intput output tasks
eg: cin & cout

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
	char a[80];
	std::cout<<"\nEnter your Name : ";
	std::cin>>a;
	std::cout<<a;
	return 0;
}

here in this example we use cin & cout

for this
#include <iostream>
is used.
<iostream> includes the following functions:

1
2
3
4
5
6
7
8
std::cin
std::cout
std::cerr
std::clog
std::wcin
std::wcout
std::wcerr
std::wclog


If you wanna use any of them, you must include the <iostream> header.
Ooh thanks a lot :)
Why? Try compiling a program that uses standard out/in without it.

Including iostream introduces some identifiers into the namespace std, all listed here:

http://en.cppreference.com/w/cpp/header/iostream
Why? Try compiling a program that uses standard out/in without it.
1
2
3
4
5
#include <cstdio>

int main() {
    std::printf("Hello, world!\n");
}
<iostream> includes the following functions:

1
2
3
4
5
6
7
8
std::cin
std::cout
std::cerr
std::clog
std::wcin
std::wcout
std::wcerr
std::wclog
Almost, but all of these aren't functions, they are objects of type ostream or istream, not functions.

when or why should i use iostream library!?
Basically every time you want inputs and outputs.

The iostream library has a very nice and typesafe interface.
It also has some fancy stuff to align your text to the right or left or to ignore whitespaces in strings.
You can set if you want your numers printed as binary, decimal or hexadecimal number.
You can set it to print bools as "true" or "false" instead of 1 or 0.
In addition to that you can make your objects streamable pretty easily (just overload operator<<) which is very nice.
If you want to change the output device you just have to set rdbuf to whatever buffer it should write.

This may have gone pretty deep but these are a few advantages iostream has over cstdio.
You may be able to achieve one or the other of them with cstdio as well but it wont be that pretty.
Last edited on
@nchambers
That's C, not C++.
@IWishIKnew Nope it's C++
closed account (E0p9LyTq)
That's C, not C++.
<stdio.h> is C, <cstdio> the C++ encapsulated header.

http://www.cplusplus.com/reference/clibrary/
The C++ library includes the same definitions as the C language library organized in the same structure of header files, with the following differences:

Each header file has the same name as the C language version but with a "c" prefix and no extension. For example, the C++ equivalent for the C language header file <stdlib.h> is <cstdlib>.
C++ allows you to create user defined types that can be used as easily as built in types. So the standard library creates a string type that can be used as easily as an int.

The missing link is I/O. The iostream library allows I/O on user defined types. printf has a fixed format string and cannot extended. So it's unsuitable for user defined types.

An added bonus of the iostream library is you deal with streams. So the class can write to the console, a file, a memory buffer, a network stream, ... without change.
@nchambers
That's C, not C++.

a) If I did write that as a C file it would still be valid c++
b) what I wrote is definitly not C and will not compile as C.
c) you need to look more clearly because that is definitly c++.
Wishing for somthing doesn't make it so. I encourage you to read the documentation: www.cppreference.com

Do no troll.
who's wishing? std::printf is C++ code. I encourage you to learn the language C before making dangerous assumptions as to what constitutes a C file.
nchambers std::printf is the C function imported to the C++ standard library. It has all the features/problems/benefits of C's printf (because it's the same function).
It is true that C++ has been derived from C. It is true that (some) language constructs are still identical. It is true that the C++ standard library still retains C-based components, even though there are already new, C++-style replacements. That means some redundancy.

It is true that a valid C++ program can do some I/O without the streams. (One can have a GUI program that does not do any console I/O.) However, the streams-based part of the standard library is, as already stated, very useful and preferable to the cstdio.

The short program by nchambers does compile without errors with a C++ compiler and produces defined and expected output. A C compiler would complain about the unknown header file and the "std::" part on identifier. In spirit, even if not in syntax details, the code is still what a C-programmer would write.

Particularly on "beginner homework", we see a lot of "legal" constructs that aren't "in the spirit" of modern C++. A lot of that is for "educational purposes". It could be that the teaching material has been inherited from C-courses with minor modifications, or that the "learning the simple, low-level basics" is still considered the most efficient way to learn to think. For reason than other std::sort is not the expected answer to "how to sort?"


However, the debate on whether syntactically correct C++ program is indeed "proper" C++ isn't answering the OP's question.
hey @khaoula! this conversation could go on and on. Please just close the topic
nchambers std::printf is the C function imported to the C++ standard library. It has all the features/problems/benefits of C's printf (because it's the same function).

two languages can share a code base. I can write nim code that will compile as python code. that doesn't make it nim code when I run it through python(1). I'm not even going to get into ffi's

It is true that a valid C++ program can do some I/O without the streams. (One can have a GUI program that does not do any console I/O.) However, the streams-based part of the standard library is, as already stated, very useful and preferable to the cstdio.

The short program by nchambers does compile without errors with a C++ compiler and produces defined and expected output. A C compiler would complain about the unknown header file and the "std::" part on identifier. In spirit, even if not in syntax details, the code is still what a C-programmer would write.

all true. I would never use <cstdio> over <iostream> in c++. It was just to show iwishiknew that his statement was wrong about needing iostream to write to stdout.

but yes this is another pointless debate and OP was already answered so I'll shut up
Last edited on
Topic archived. No new replies allowed.