Since the namespace std already has the c++ libraries that contain the function definitions(if i am right), then why do we include header files on top of it??. Since namespace std includes the c++ standard libraries, I don't see a reason to include the declarations of it separately.
-
2Not QUITE sure if you are asking "why do we need both namespaces and header files", or "why it makes a difference between order of `#include
` and `using namespace std;`? – Mats Petersson Sep 28 '14 at 16:11 -
2You should never include `iostream.h` unless your program requirements include working on DOS. – chris Sep 28 '14 at 16:11
-
1saying you are using a namespace is in no way the same as saying you have access to all the classes included in that namespace. So, essentially you are asking why you need to include header files to access the classes that are declared there? – Benjamin Trent Sep 28 '14 at 16:16
-
1`iostream.h` looks weird. The current standard refers to [`#include
`](http://en.cppreference.com/w/cpp/header/iostream) actually. – πάντα ῥεῖ Sep 28 '14 at 16:18 -
Why should you? You shouldn't. It would achieve nothing. – juanchopanza Sep 28 '14 at 16:21
-
@chris So UNIX programs shouldn't include `iostream`? ;-) – Carey Gregory Sep 28 '14 at 16:42
-
1@CareyGregory: In Unix you probably don't have a `iostream.h` - note the distinction between `iostream` and `iostream.h`. – Mats Petersson Sep 28 '14 at 16:43
-
@MatsPetersson You don't have it with any version of a Microsoft compiler either (that I know of) other than the headers that come with the WDK, which very few people would have. – Carey Gregory Sep 28 '14 at 16:45
-
Right, so chris said that "you shouldn't be using it unless you are compiling for DOS", which is exactly what you are saying - it doesn't exist/shouldn't be used, unless you are using a compiler that is for an OS that stopped being used some 15 or so years ago... – Mats Petersson Sep 28 '14 at 16:48
-
@MatsPetersson I understood what he's saying. I just think the OP used the `.h` by accident, so my comment was a joke. See the smiley? – Carey Gregory Sep 28 '14 at 18:43
-
_"Since namespace std includes the c++ standard libraries"_ Nope – Lightness Races in Orbit Apr 12 '15 at 14:05
5 Answers
When you do #include <iostream>
it causes a set of classes and other things to be included in your source file. For iostream, and most of the standard library headers, they place these things in a namespace named std
.
So the code for #include <iostream>
looks something like this:
namespace std {
class cin { ... };
class cout { ... };
class cerr { ... };
class clog { ... };
...
}
So at this point, you could write a program that looks like:
#include <iostream>
int main() {
std::cout << "hello\n";
return 0;
}
Now, some people feel that std::cout
is too verbose. So they do:
#include <iostream>
using namespace std;
int main() {
cout << "hello\n";
return 0;
}
Personally, I'd recommend against this, and if you really feel that std::cout
is too verbose, then I'd suggest that you use a smaller using
statement.
#include <iostream>
using std::cout;
int main() {
cout << "hello\n";
return 0;
}
If you're wondering why I would recommend against using namespace std
, then I would forward you to the following two other posts on stackoverflow:

- 1
- 1

- 80,138
- 16
- 128
- 173
-
1So " using namespace std; " and then " cout<<"somestring"; " would still refer to the cout in the "
" file? even without including the – Manoj Bharadwaj Sep 28 '14 at 17:01file? because inside the file these cout, cin are placed inside namespace std, so can we just use std namespace and leave out the header inclusion? -
@ManojBharadwaj: No. Many different header files will place things in that namespace. The `using` command doesn't implicitly know which header that is. – Bill Lynch Sep 28 '14 at 17:54
-
but there is only 1 object by that name inside a single namespace, isn't that enough? – Manoj Bharadwaj Sep 28 '14 at 18:18
-
But how does the compiler know what `cin` is or does? That comes from the content of iostream. The compiler itself does not know what this file contains, or for that matter that `cin` comes from `iostream`. This is part of the runtime library, not the compiler itself, so it must be COMPILED by the compiler for your program to be able to use it. – Mats Petersson Sep 28 '14 at 18:25
The compiler itself does not have the definitions of the things that are in any namespace (whether it is std
or some other namespace). That is the role of source files and header files.
What using namespace std;
tells the compiler is that "If you can't find some name in the current namespace, go look in the std
namespace as well".
What #include <iostream>
tells the compiler is that you want the contents of the header called iostream
to be included in your sources. This will provide the compiler with code to do cin
, cout
and a lot of other related functionality. The content of this file is declared like namespace std { ... all the stuff goes here ... }
.
The usage of namespace allows someone else, working in namespace math;
to not have to worry about "Hmm, what do I do now, I need a counter for number of entrances, let's call it cin
- but hang on, is that ever used anywhere?".
This may not be the greatest of examples, but in large projects, it gets increasingly hard to keep track of things and what names they have. And C++ is a language intended for large projects of millions of lines of code - and now it starts getting hard to remember if you've used a particular name or not. Namespaces make sure that you don't have to worry about it outside of a particular namespace.
(Oh, and in my code, I tend to not use using namespace std;
, but write std::cout << "Hello, World!" << std::endl;
- this helps to make it clear that the cout
I'm using here is the std
one, and not something else. This is particularly useful when you have several namespaces with similar things, like in my own compiler, where I have my compiler with it's functionality, the std
namespace providing some things, and llvm
compiler things - if I were to stick using namespace llvm;
at the beginning of the code, it would be very hard to track whether Type* p = ...
; is from LLVM or some part of my own code.)

- 126,704
- 14
- 140
- 227
...why do we include header files on top of it???
Yes, there is some big confusion here.
Namespaces
Namespaces are a method to categorize or group together, symbols such as function names.
Namespaces are design to prevent name conflicts between different software components, such as libraries.
Functions that are a part of the standard language, are grouped under the namespace std
.
The C++ language provides statements to reduce the amount of typing when using namespaces. One of these is the using
statement.
Header (include) Files
When you write a program, the compiler is not required to automatically include all the symbol definitions, such a function declarations. You need to tell it which functions you plan on using.
For example, I could write a program without using the sort
or advance
functions from the algorithm
group. Therefore I would not include the header file algorithm
.
The C++ language is designed to be "use what you need", in other words, we can create small programs by only including the functions we need.
Other Platforms
By the way, there are many other platforms out there than the one you are using.
Some platforms need to fit in small memory areas and may not have a keyboard or display (such as embedded controllers).
So remember, C++ was defined to support platforms from the small and constrained to the large and virtually unconstrained system.
Thus the requirement to "include only what you need" theme.
Summary
In summary, since the C++ languages doesn't automatically, magically, provide the definitions of the entire library, including the template library, you need to tell the compiler which groups of functions you want to use. This allows for quicker compilations since only the required header files are specified.
Note: Some shops and library supplies like to use the Monolith include system. This means that they have one include file that includes their entire library, whether you use one function or many. The windows.h
is a classic example. One detriment is that when one header file is changed, everything needs to be rebuilt. With separated include files, only the components that include the changed header file need to be rebuilt.

- 56,849
- 17
- 98
- 154
Use of preprocessor directive #include is as old as c++ itself. And its not going away any sooner.In C++ namespace Doesn't import anything into your program, it just defines the scope of your particular header file's function.So, both are required. Click hereto understand why use namespace.
Your question is: namespace std
has all the definitions of functions/classes of iostream
library. So simply using using namespace std
is enough to call or use cout
or all other functionalities of iostream
library. Why do we have to use line #include <iostream>
? It seems redundant.
Roughly we can think that iostream
library has two files: header and implementation/source file. These two files have a namespace called std
. The header file only contains the declarations or forward declarations of classes or functions or variables that iostream
library is going to use and these declarations or forward declarations are under the std
namespace. The implementation file contains the actual implementations of the classes or functions or variables and these implementations are under the std
namespace; this file is also called the source file.
So if you only use using namespace std
without #include <iostream>
in your main.cpp
file compiler will search std
namespace in your main.cpp
file, but it is not here. You will get compiler error.
Now if you include this #include <iostream>
line in your main.cpp
, preprocessor will go iostream
's header file and copy the std
namespace along with its code into our main.cpp
. And linker will link the precompiled(as iostream
is a SLT so it comes with compiler with prebuilt) source/implementation file of iostream
to your main.cpp
. Now to use functions or variables of iostream
that sit under std
namespace in main.cpp
we have to use scope resolution operator(::) to tell the compiler that cout
, cin
and other functionalities of iostream
sit at std
namespace. Thus we simply write like this: std::cin
, and std::cout
.
Now typing std::
seems redundant to some people so they tell the compiler by using this using namespace std
"Hey compiler, if you don't find any variables/functions/classes in global/current namespace go look in the std
namespace." Though this is not the best practice but that is another topic to discuss. Therefore your assumption that std
namespace contains all the definitions of all SLT's functions/classes/variables of C++ is not correct in most cases, but std
namespace only contains the declaration from STL is the correct assumption.
Here is a dummy implementation how iostream
libray is added to our code files:
iostream.h
:
// This is header file that only contains the
// functions declarations.
namespace std_dum
{
int add(int x, int y);
int mult(int x, int y);
}
iostream_dum.cpp
:
// This is source file of iostream_dum.h header
// which contains the implementation of functions.
#include "iostream_dum.h"
namespace std_dum
{
int add(int x, int y)
{
return x + y;
}
int mult(int x, int y)
{
return x * y;
}
}
main.cpp
:
#include <iostream>
#include "iostream_dum.h"
int main()
{
std::cout << std_dum::add(100, 200) << '\n';
std::cout << std_dum::mult(100, 200) << '\n';
return 0;
}
To see what happens to our main.cpp
file after preprocessing run this command: g++ -E main.cpp iostream_dum.cpp
.
Here is roughly our main.cpp
looks like:
namespace std_dum
{
int add(int x, int y);
int mult(int x, int y);
}
int main()
{
std::cout << std_dum::add(100, 200) << '\n';
std::cout << std_dum::mult(100, 200) << '\n';
return 0;
}
namespace std_dum
{
int add(int x, int y)
{
return x + y;
}
int mult(int x, int y)
{
return x * y;
}
}
For the sake of clarity, I discarded all the codes that the preprocessor copied from #include <iostream>
.
Now it should be pretty clear.

- 153
- 1
- 1
- 9