Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

For which purpose "using namespace std;" is use in C++ program?

+2 votes
asked Feb 14 by osama asif (140 points)

1 Answer

0 votes
answered Feb 17 by Peter Minarik (19,180 points)
using namespace [namespace]

Allows you to omit the [namespace] from the fully qualified name.

For instance, if you'd want to print something to the screen, you'd need something like the following code:

#include <iostream>

int main()
{
    std::cout << "Hello" << std::endl;
    return 0;
}

As you can see the output stream cout is referred to with its fully qualified name including the namespace std as well: std::cout.

If you are feeling lazy to type the namespace all the time you can use the using namespace std statement, which allows you to not to write only the output stream without the fully qualified name.

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello" << endl;
    return 0;
}

Notes

  1. There could be naming clashes (that's why there are namespaces in the first place), so you may not always be able to use a namespace in your code.
  2. You must not use namespace pollution, that is, apply the using namespace instruction in header files as anyone who includes this file will have the same using applied, even though it may not be desired (see point 1 above).

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...