Use of "using namespace std"

What is the use of "using namespace std" in c++ programme.What difference does it make with a normal programme.I use Turbo c to compile my c++ programme but it does not consider using namespace std as a keyword and moreover I find it difficult to understand about this without compiling and checking it.Can i know a alternate software for compiling my c++ programme where this is considered as a keyword.Another problem with this is I could not figure out how to leave line using this.In my earlier days of programming I used "cout<<endl;" or "cout<<'\n';" after using this keyword I tried std::cout<<"\n"; but there is some syntax error.Can anyone help me out with these problems.
Thanks in advance.
using namespace std; is useful to make bad C++ programs. Don't use it.

http://stackoverflow.com/q/1452721/1959975
Last edited on
I use Turbo c to compile my c++ programme but it does not consider using namespace std as a keyword


Update your compiler. Borland Turbo C++ is ancient. Like... pre-standard ancient. It's a dinosaur. It's not even really C++.

What is the use of "using namespace std" in c++ programme.


cout, string, etc -- all the identifiers you are used to using are inside of the "std" namespace. Having them in a separate namespace prevents name conflicts.

IE: if you create your own class named string, it would conflict with the standard class with the same name, which might lead to linker errors.

So, having them in a separate namespace prevents those conflicts. The class is no longer just named "string". Now it is "std::string"

1
2
3
4
5
6
7
8
#include <string>

int main()
{
    string foo;  // <- error, what is 'string'?  There is no 'string'!

    std::string bar; // <- OK.  You mean the 'string' that is inside the std namespace.  Gotcha
}



The line using namespace std; takes the entire std namespace (cout, string, vector, map, etc, etc, etc) and dumps them all into the global namespace... effectively removing the namespace entirely. This allows you to shortcut typing the names, but also means you have the possibility for name conflicts (since they're no longer in a separate namespace:

1
2
3
4
5
6
7
8
9
#include <string>
using namespace std; // <- take everything in the std namespace (like std::string)
   // and bring it into the global namespace

int main()
{
    string foo;  // <- OK.  Because 'string' is now recognizable since we dumped std::string
       // into global namespace
}




after using this keyword I tried std::cout<<"\n"; but there is some syntax error.


Again... this is because your compiler is so old that it is not really compiling C++. It's compiling some unstandardized language that is kind of similar to C++.... but isn't actually C++.

Update your compiler.
Thank you for your help
Topic archived. No new replies allowed.