40

As I started learning basic C++, I've always used the headings

#include <iostream>
using namespace std;

I want to question what is the point of iostream. Is it required every time as a heading?

William
  • 411
  • 1
  • 4
  • 7

4 Answers4

48

In order to read or write to the standard input/output streams, you need to include it.

int main(int argc, char * argv[])
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

That program will not compile unless you add #include <iostream>

The second line isn't necessary:

using namespace std;

That does tell the compiler that symbol names defined in the std namespace are to be brought into your program's scope, so you can omit the namespace qualifier, and write for example:

#include <iostream>
using namespace std;

int main(int argc, char * argv[])
{
    cout << "Hello, World!" << endl;
    return 0;
}

Notice you no longer need to refer to the output stream with the fully qualified name std::cout and can use the shorter name cout.

I personally don't like bringing in all symbols in the namespace of a header file... I'll individually select the symbols I want to be shorter... so I would do this:

#include <iostream>
using std::cout;
using std::endl;

int main(int argc, char * argv[])
{
    cout << "Hello, World!" << endl;
    return 0;
}

But that is a matter of personal preference.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amdn
  • 11,314
  • 33
  • 45
15

That is a C++ standard library header file for input output streams. It includes functionality to read and write from streams. You only need to include it if you wish to use streams.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
12

# indicates that the following line is a preprocessor directive and should be processed by the preprocessor before compilation by the compiler.

So, #include is a preprocessor directive that tells the preprocessor to include header files in the program.

< > indicate the start and end of the file name to be included.

iostream is a header file that contains functions for input/output operations (cin and cout).

Now to sum it up C++ to English translation of the command, #include <iostream> is:

Dear preprocessor, please include all the contents of the header file iostream at the very beginning of this program before compiler starts the actual compilation of the code.

Ramzan
  • 191
  • 2
  • 11
  • 1
    Files specified by `#include` are transcluded at the point of the `#include`, not the start of the file – CSM Mar 07 '20 at 16:33
  • to be super verbose.. also note: https://stackoverflow.com/questions/3162030/difference-between-angle-bracket-and-double-quotes-while-including-heade – nayana Mar 31 '21 at 11:42
0

#include where as is the including the functionsfor the input AND output (cin and cout) operations

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 03:47