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
How to Run a C++ Program Without Namespace? Prerequisite: Namespace in C++ If we want to run a program without using a namespace, we have to the std keyword along with the space resolution operator (::) in every printing line and variable declaration, For example, std::cout<<"geeksforgeeks"; Example: C++ // C++ Program without the use o
1 min read
When to Use Enum Instead of Macro in C++? In C++, both enums and macros are used to define symbolic names for a set of values. However, there are certain situations where using an enum is more beneficial than using a macro. In this article, we will learn when to use an enum instead of a macro in C++. When to Prefer Enum Instead of Macro?In
3 min read
Why C++ is best for Competitive Programming? C++ is the most preferred language for competitive programming. In this article, some features of C++ are discussed that make it best for competitive programming. STL (Standard Template Library): C++ has a vast library called STL which is a collection of C++ templates to provide common programming d
4 min read
What are the Rules for Using an Underscore in a C++ Identifier? In C++, an identifier is a unique name that is used to define a variable, function, class or object, and etc in a program. It can consist of numbers, letters, and underscores and must start with an underscore or letter (can be uppercase or lowercase). No special character is allowed in identifiers.
2 min read
Namespaces in C++ | Set 4 (Overloading, and Exchange of Data in different Namespaces) Prerequisite: Namespace in C++ | Set 1 (Introduction)Namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing) In this article, we will discuss the concept of data sharing between different namespaces, how to acces
5 min read
C++ Program to Show Use of This Keyword in Class Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. There are 4 wa
4 min read
When to Use Vector Instead of Array in C++? In C++, both arrays and vectors are used to store collections of data. Arrays are a basic fixed-size sequence of elements, whereas vectors are part of the C++ STL and offer dynamic sizing and more flexibility. There are some cases where using vectors can be more beneficial than using arrays. In this
4 min read
How can we write main as a class in C++? As it is already known that main() method is the entry point in any program in C++, hence creating a class named "main" is a challenge and is generally not possible. But this article explains how to write a class named "main" in C++. What happens when we try to write a class named main? Writing a cl
3 min read
Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo
5 min read
What is an Undefined Reference Error in C++? In C++, an undefined reference is an error that occurs when the linker can't find the definition of a function or a variable used in the linking stage of the compilation process. This error needs to be resolved for the smooth working of the program. When Does Undefined Reference Error Occur in C++?A
2 min read