Why do we use "using namespace std ;" in c++?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

Why do we use "using namespace std ;" in c++??

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

29th May 2019, 3:12 PM
Akash Gupta
Akash Gupta - avatar
8 Answers
+ 10
If you don't specify the namespace then you're going to have to write std:: Before any library function/data type/variable in iostream. e.g. cout<<"Hello, Akash"; Becomes this without namespace std std::cout<<"Hello, Akash"; It's not required to always use "using namespace std;" when using iostream especially if there's some other library with another namespace you're going to use more but if you're going to use a lot of iostream throughout your code then it'll make your code look a little cleaner to use namespace std
29th May 2019, 4:15 PM
Earl Chukwu
Earl Chukwu - avatar
+ 8
I read from the experts that you actually really want to avoid that! Using std:: before an io statement reduces the chance for code breaking, even if it's overall more work. At least that's both what I've heard and seen.... Just make sure you include iostream.
30th May 2019, 9:31 PM
Ian Hodges
Ian Hodges - avatar
+ 6
iostream library requires a namespace to identify classes. writing namespace std you will tell the compiler you’re using the standard namespace
29th May 2019, 3:33 PM
Gabriele Concli
Gabriele Concli - avatar
+ 5
Imagine that exists more of one namespace format. Then you have to specify which one you want to use for your outputs. As #Earl Chuwu said: " it'll make your code look a little cleaner to use namespace std". Peace.
30th May 2019, 2:15 PM
Raffaele Alfano
Raffaele Alfano - avatar
+ 1
it help us to avoid repeated use of std::w/c make our program unreadable
1st Jun 2019, 11:41 AM
Asrat Adane
Asrat Adane - avatar
+ 1
You don't have to use it. It just makes your code easier to write. "iostream" library has a class that is called "std". The code "using namespace std;" tells your compiler that if you find a method being called that is in "std" class, then immediately use method in that class. Thus by not having the "using namespace std;" in your code, then you have to use "std :: cin" or "std :: cout" for it to work or the compiler will throw a Syntax Error. However if I have a class named "Test", and in that class a method named "cin", when I use the "cin" method in the main function, then it would call the "std :: cin" method not the "Test :: cin" method. But if I call the "Test :: cin" method, then it would call my class' method.
5th Jun 2019, 3:25 PM
Arashk
Arashk - avatar
+ 1
Arashk what or how many classes are there for a compiler to use? And how do I know which to use?
20th Jun 2019, 5:22 PM
christopher mccabe
0
christopher mccabe Look there are a lot of libraries that you can include. Sometimes those are necessary for some codes. I believe that the only class in "iostream" is "std". you can include as many libraries. You can just search in Google to find out which library has which classes that you need. You can include as many libraries as you want but it would slow down the program if you're working on projects. I haven't used any other environment but in Codeblocks you can just put "#include <bits/stdc++.h>" at first to include every library at once but again I don't know if it works with other environments or not and it slows down and make the program heavier.
18th Aug 2019, 12:18 PM
Arashk
Arashk - avatar