what is the difference between the "iostream" library and "using namespace std;" in c++

I am new to C++. I can't get the difference between `iostream` library and `using namespace std;` because `iostream` library are two types named `istream` and `ostream`, which represent `input and output streams`, respectively. And `namespace std` is the `standard namespace`.` cout`, `cin` and a lot of other things are defined in it. (This means that one way to call them is by using `std::cout` and `std::cin`.)

So my question is, what is the need of defining both `iostream` and `using namespace std;` in a program?
Sourodip KunduAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Sourodip Kundu,

these two statements are completely unrelated.

1.) std is a namespace. Namespaces are used to put related functions together in any meaningful way, i.e. grouped by functionality or by library or anything. See the sample code below to see how it can be used.

Everything which is not declared within any namespace is part of the unnamed, so called global namespace ::. This is i.e. needed to access objects/functions from within a named namespace.

With using namespace a namespace can be made available in the current namespace, this means all functions/objects from such a namespace can be used in the current namespace too.

Important hint: Many experts recomment not to use using namespace std;, at least not on a global scope (not within a functions, a class declaration, or another namespace) for different reasons - there are a lot of discussion in the Internet, i.e. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

2.) libraries: Libraries are collection of functions and/or objects which can be used in your program. The default (and most important) library is the STL (Standard Template Library) which offers really much for wide spreaded needs, i.e classes for containers, strings, files, streams, many more, ... and functions/algorithms.

STL is a header-only library, so all you need to do to use it is to include the appropriate headers. JFI: Beside header-only libraries it's even possible to build/use libraries with pre-built binaries (called .LIB).

#include <iostream> // make some STL functions/classes available

void foo() {
 std::cout << "foo() in global namespace called\n";

namespace my {
  using namespace std;
  void foo()  { cout << "my::foo() called\n"; } // no 'std::' needed with 'cout' - but see comments about this below
  void bar()  { ::foo(); }

int main()
  foo(); // calls global ::foo()
  my::foo(); // cally my::foo()
  bar(); // error, there's no global ::bar()
using namespace my;
  bar(); // ok, namespace 'my' is known now

Open in new window

This is a very short and simplified description, I hope it helps anyway.

Best regards,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sourodip KunduAuthor Commented:
Thank you, I got it
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.