Why is 'using namespace std' used

Hi,

I'm just starting to learn C++.
What I've learnt till date is we use <iostream> which contains everything needed for input/output.
Then also we need to type 'using namespace std' otherwise the code throws an error.

So my question is what is the necessity of using this namespace if everything is there in <iostream> for cout, cin etc..

I searched the net but everywhere the only thing i could find is the definition of 'namespace' and that 'namespace std' is a 'standard namespace'.

I would be glad if any of u Gurus kindly clarify my doubt. Many thanks in advance!

Regards,
Jaya
closed account (Gz64jE8b)
Without using namespace std; when you write for example cout <<; you'd have to put std::cout <<;

Here's a small example for you:

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

int main()
{
    std::cout << "Hello World";
    system("pause");
    return 0;
    
}


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

using namespace std;

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


They both do the exact same thing.
Let's start with a problem to explain what namespaces are. We all know that we can't have functions, classes or any other kind of data that have the same name. Let's say that we have two libraries that both add a function, let's say print(). They both give a different function, but in naming they are indistinguishable. That's where namespaces come in. A namespace is like adding a new group name to which you can add functions and other data, so that it will become distinguishable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace bla
{
  void print()
  {
    // function bla::print()
  }
}

namespace blabla
{
  void print()
  {
    // function blabla::print()
  }
}


Now, as you can see, there are two functions with the name print, yet there is no naming conflict, because of namespaces. The first function would be called by using: bla::print() and the second with blabla::print().

std is an abbreviation of standard. std is the standard namespace. cout, cin and a lot of other things are defined in it. (This means that one way to call them is by using std::cout and std::cin.)

The keyword using technically means, use this whenever you can. This refers, in this case, to the std namespace. So whenever the computer comes across cout, cin, endl or anything of that matter, it will read it as std::cout, std::cin or std::endl.

When you don't use the std namespace, the computer will try to call cout or cin as if it weren't defined in a namespace (as most functions in your codes). Since it doesn't exist there, the computer tries to call something that doesn't exist! Hence, an error occurs.

If you have any trouble understanding the above, please tell me and I will try to clarify.


Edit:
This is the tutorial page that explains what I said a lot better (with some simple examples):
http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
Mohammad Abdul and Kyon...thank you so much both of you :)

But I'm still having one doubt...it might sound stupidic to Gurus like you..

If cout, cin, endl etc are defined in the 'namespace std' then why are we using both 'namespace std' and <iostream>? I mean we can use any one of them. Whats the use of using both of them in a simple program like 'Hello world'.

My question will sound stupidic to you but I would really appreciate if you explain it. Many thanks in advance!
That isn't at all a stupid question.

iostream is a C++ code file (more specifically a header file, but that doesn't matter). It contains the code that defines cout, cin, endl, etc. If we want to use them, we need them to be in our code, that's why we include it. However, if we look at it's contents, we will (schematically) see something like this (This is not a serious attempt of trying to map iostream schematically, but I will refrain from making things too complicated, and for the explanation, it will do):
1
2
3
4
5
6
7
8
// iostream header file

namespace std
{
   ostream cout;
   istream  cin;
   // etc etc
};


Using the using keyword doesn't mean we add functionality, it means we say that we read things by default. If we say using namespace std; then we say: If we come across an object name that doesn't exist in our current namespace, check if there exists a namespace std in which it does exist, and use that object. Thus, it doesn't really add a function, it is the include <iostream> that "loads" cout, cin, endl and all the like.

(Note that the schematic above is not what you will find when opening the iostream file, in reality it's a lot more complex and also contains a lot of definitions.)
Topic archived. No new replies allowed.