Why it is important to write "using namespace std" in C++ program?
Last Updated :
16 Jul, 2024
In this article, we will discuss the use of "using namespace std" in the C++ program.
As the same name can't be given to multiple variables, functions, classes, etc. in the same scope. So, to overcome this situation, namespace is introduced.
Example
Below is the C++ program illustrating the use of namespace with the same name of function and variables:
C++
// C++ program to illustrate the use
// of namespace with same name of
// function and variables
#include <iostream>
using namespace std;
// Namespace n1
namespace n1 {
int x = 2;
// Function to display the message
// for namespace n1
void fun() { cout << "This is fun() of n1" << endl; }
} // namespace n1
// Namespace n2
namespace n2 {
int x = 5;
// Function to display the message
// for namespace n2
void fun() { cout << "This is fun() of n2" << endl; }
} // namespace n2
// Driver Code
int main()
{
// The methods and variables called
// using scope resolution(::)
cout << n1::x << endl;
// Function call
n1::fun();
cout << n2::x << endl;
// Function call;
n2::fun();
return 0;
}
Output2
This is fun() of n1
5
This is fun() of n2
Explanation:
- In the above example program, both n1 and n2 have a variable and function of the same name x and fun() respectively.
- The namespace is used to decrease or limit the scope of any variable or function.
- As in the above code variable x and method fun() were limited to namespaces n1 and n2. Thus, their scope was not outside the n1 or n2.
Need of using for Namespaces
Every time we use the identifiers defined inside a namespace in another scope, we need to use the scope resolution operator (::) in a variable or a function. But we can avoid it by utilizing the "using" directive.
The using directive makes the declarations and definitions of the given namespace visible in the current scope.
Example
Below is the C++ program demonstrating the use of the "using" directive:
C++
// C++ program to demonstrate the use
// of "using" directive
#include <iostream>
using namespace std;
// Namespace n1
namespace n1 {
int x = 2;
void fun() { cout << "This is fun() of n1" << endl; }
} // namespace n1
// Namespace is included
using namespace n1;
// Driver Code
int main()
{
cout << x << endl;
// Function Call
fun();
return 0;
}
Output2
This is fun() of n1
Explanation:
- In the above program, after writing "using namespace n1", there is no need to use the scope resolution for utilizing the members of n1.
- It can be interpreted as "using" tells the compiler to not only look for x and fun() in the current scope, but also look for them in the namespace n1 where the compiler successfully find it.
Also, "using" only makes the namespace visible in the scope where it is used. For Example, if "using namespace n1" is written inside the main() and we try to use the members (fun() and x in this case) in the different functions, it would give a compile-time error.
Note: The compiler will only look up in the given namespace only if the identifier is not found in the current scope.
Importance of "using namespace std" in C++
It is known that "std" (abbreviation for the standard) is a namespace where all the C++ Standard Library Functions, Classes and other stuff is declared. So, the members of the "std" namespace are cout, cin, endl, etc.
So, to avoid the usage of scope resolution operator with std namespace for every standard library component, we use the statement "using namespace std" to make the compiler look for the given identifier in the std namespace.
Example: Not Adding "using namespace std" in our code
If we don't use the "using namespace std" statement in our code, our code will look like this"
C++
// C++ program to illustrate
// the use of std
#include <iostream>
// Driver Code
int main()
{
int x = 10;
std::cout << " The value of x is " << x << std::endl;
return 0;
}
Output The value of x is 10
Explanation: The output of the program will be the same whether write "using namespace std" or use the scope resolution.
Note: It is recommended to not use the "using namespace std" in your development projects as you may use the different identifiers declared inside different namespaces and the before statement will make the std namespace visible in all of the global scope.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and
9 min read