what is using namespace std;

why do we need to write using namespace std; or std:: in every C++ program?
what is it's purpose?
It is a namespace scope resolver. Basically, what it means is that we are accessing the functions that have been defined in the std namespace. Namespaces are used so that you don't get multiple definitions of a class or function, which can cause wierd linker errors. The using namespace std; is used to get the compiler to behave as if the std namespace doesn't exist, and all functions are declared in the global namespace. However, it is often preferable (especially in header files) to use std::funcname, as an explicit reference saying 'I want the funcname in the namespace std'.
It makes the namespace visible, all the functions and variables like endl become visible. This is not a good idea because lets say you had another library that you wanted to use, and it had a class called string. Now when you type string myName you don't know which one is being used. With the scope resolution modifier operator ( :: ), you can distinguish between them.
1
2
std::string nameOne;
myLib::string nameTwo;
Last edited on
thanks @NT3 and vasilenko93
i understood the use.
is it in C++ only or it's included in C too?
closed account (Dy7SLyTq)
no c doesnt have namespaces because i dont believe it has the scope operator. it is in c# however
c has the scope operator but it shows error on adding std:: to some functions.
ya, thanx
Topic archived. No new replies allowed.