VOOZH about

URL: https://www.geeksforgeeks.org/cpp/why-it-is-important-to-write-using-namespace-std-in-cpp-program/

⇱ Why it is important to write "using namespace std" in C++ program? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Why it is important to write "using namespace std" in C++ program?

Last Updated : 23 Jul, 2025

In this article, we will discuss the use of "using namespace std" in the C++ program.

Need of Namespace in C++

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:


Output
2
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:


Output
2
This is fun() of n1

Explanation:

  • In the above program, after writing "using namespacen1", 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"


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.

Comment
Article Tags: