What is a namespace?

using namespace std;

As a C++ programmer, one often encounters the above statement i.e. using namespace std; below the header file declarations in C++ programs. Ever wondered why this statement is used? Well, you would have guessed this statement has something to do with a namespace that has the name std. What is a namespace, by the way? Why do we need namespaces? Here we shall explain all these things.



What is a namespace?

The term namespace refers to the mechanism used for logical grouping of identifiers i.e. the names of functions, variables etc.

A namespace is declared as :

namespace nmsp
{
data-type2 var1;
data-type2 var2;
return-type1 func1 () {}
return-type2 func2 () {}
}

Here, nmsp is the name of the namespace we’ve defined; var1, var2 are two variables and func1 and func2 are two functions defined inside this namespace.

For example, a sample namespace would be like

namespace maths
{
double x;
double y;
double add (double a, double b) {return (a+b);}
double subtract (double a, double b) {return (a-b);}
double multiply (double a, double b) {return (a*b);}
double divide (double a, double b) {return (a/b);}
}

When you include a statement like using namespace std; in your program, you’re specifying that the contents of that namespace would be available for use in that program without any prefix. If you don’t include this statement in your program, the identifiers that require this namespace will have to be accompanied by the prefix std::.

Similarly, for other namespaces, the required prefix would be <namespace-name>::. In other words, a namespace named wcb requires its identifiers to have the prefix wcb:: (in case using namespace wcb; hasn’t been used in the program). See the following two examples to understand this.

Example 1 – Without “using namespace std;”

#include <iostream>
int main()
{
cout<<“Hello World”;
return 0;
}

The above example wouldn’t work. Why? cout is a part of the std namespace and you haven’t included std in your program. You would have to replace cout with std::cout in order to make your program work.

Example 2 – With “using namespace std;”

#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World”;
return 0;
}

The above program would work properly because you have included the required namespace.


Why do we use namespaces?

This is the most important question concerning namespaces. Why exactly do we need namespaces?

There are mainly two reasons you might want to use namespaces.

  • First, namespaces can be used to group identifiers. Therefore, they can be used to group similar functions, say, user input functions in one namespace and output showing functions in another. For example, functions like getdata(), getval(), getinput() etc. can be put in a namespace named userinput. Similarly, functions like showdata(), putval(), output() etc. can be put in a namespace named output.
  • Second, they are used to prevent conflicts. If you’re using multiple libraries in your program, you might have to face name conflicts i.e. two or more identifiers having the same name. For example, suppose you have employed two libraries Add.lib and Subtract.lib in your program. Now, you face a problem. Both libraries have a function named Calculate(). Add.lib uses this function to add two numbers, while Subtract.lib uses it to subtract the smaller number from the greater number. Now suppose you make a function call to Calculate(). Which function will be called – the one from Add.lib or the one from Subtract.lib? To get rid of this ambiguity, we use namespaces in our programs.

How to use namespaces in a C++ program?

We have described namespaces and the reasons one should use them. But how do we use namespaces properly in our programs? It is quite simple.

There are two ways to use the using….. statement in your program.

  • The first way is to include the entire namespace. This is done by writing using namespace <namespace name>; e.g. using namespace std; using namespace wcb; etc. In this case, you have included the entire namespace, so you can write all the identifiers of that namespace without using the <namespace-name>:: prefix.
  • The second way is to include only the identifiers you need. This is done by writing using <namespace-name>::identifier-name; e.g. using std::cout; using std::cin; etc. In this case, you have included only individual identifiers from a namespace, so you have to use the <namespace-name>:: prefix with all other identifiers.

Many programmers recommend that you avoid the use of using…. statements. They say you should always write the full name of an identifier, such as std::cout.




In my personal opinion, it is appropriate to use using namespace <namespace-name>; especially for long programs. The reason is obvious : Writing using namespace … statements makes it easier to focus on the actual coding process rather than having to remember all the namespaces and the identifiers they contain.

We hope you liked reading this article. If you did, please let us know. 🙂

(Visited 218 times, 1 visits today)

4 comments

  • Thanks a lot for sharing!

  • Nice article. I used to wonder why I have to use to using namespace std; Earlier I used Turbo C++ and didn’t need any such thing. But ever since I started using Visual Studio, I had to use this statement or my programs won’t work. Even those who recommended me to use this using .. statement didn’t explain why I had to use it. :)Thanks for sharing such informative post 🙂

  • A very detailed explanation of the namespaces..! That helped me a lot. I was just scratching my head over the confusing std:: and using namespace std; stuff. Now, it’s all clear to me. I’m glad C++ uses namespaces so little. In c#, the whole program is based on namespaces. OMG !

Leave a Reply

Your email address will not be published. Required fields are marked *