Hello guys:

I am certain if I was not trying slef-teaching C++, that this question would have been answer before. What is the use of using namespace std; ? I am not certain, I just know that when I use #include<iostream> I have to appended. Simple question, but a matter that I need to get to the bottom of it. Thank you ,

Your rookie programmer

GCard

>What is the use of using namespace std; ?
Namespaces are like drawers in a filing cabinet. All of the standard library is in one drawer, and the only way to get to the stuff in that drawer is to tell C++ where to find it. You do that by prefixing the name with std:

#include <iostream>

int main()
{
  std::cout<<"Hello, world!\n";
}

std::cout is basically like saying "Hey C++! Look in std and get me cout , asap!". If you don't tell C++ which drawer to look in, it won't find what you want. That's why this gives you a nasty little error, C++ is complaining that it can't find cout:

#include <iostream>

int main()
{
  cout<<"Hello, world!\n";
}

Now, it's somewhat tedious to keep typing std:: in front of everything, so you can also tell C++ to assume what drawer to look in. A using declaration tells C++ to assume that the declared name is in the declared namespace:

#include <iostream>

int main()
{
  using std::cout;

  cout<<"Hello, world!\n";
}

The using std::cout part tells C++ to assume that wherever you say cout , you really mean std::cout .

Even that can get tedious, and maybe you want to say "Hey C++! Pretend that everything is prefixed with std:: ! On the double!" You can do that with a using directive:

#include <iostream>

int main()
{
  using namespace std;

  cout<<"Hello, world!\n";
}

The using namespace std part is a quick and easy way to basically create a using declaration for every single thing in the std drawer.

commented: Nicely stated. +2

Just think of namespaces as another level of organization.

If the only programs that were ever written were only one screen long, then things like namespaces probably wouldn't be necessary. However, many/most of the commercial programs out there are very long and developed by different teams of developers. This means that sooner or later someone is going to use the same name for a different purpose and thereby create a name clash that can hinder the project. namespaces are one way to decrease/sidestep that risk.

Namespaces can be used to group items that have some similar characteristics. For example, all the code that is standard to the language is in namespace std. You don't have to go to different namespaces to find standard code for strings and a different namespace to find I/O protocols, etc.

I'm sure there are other benefits as well. For now, though, just realize that as a beginner namespaces seem superfluous. For a seasoned developer they become very useful...or so I'm told.

Thank you guys. Both explanations make so much sense and actually now what it actually is, and also how to use it better. Awesome.

very super explanation.

commented: Don't bump old threads -1

agreed, Thanks for the explanation

Namespaces are a relatively new C++ feature just now starting to appear in C++ compiler
Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

The format of namespaces is:


namespace identifier
{
entities
}

>Namespaces are a relatively new C++ feature just now starting to appear in C++ compiler
If by "just now" you mean "almost two decades ago", you'd be correct. They're not new, and all decent compilers have a mature implementation in place.

>Namespaces are a relatively new C++ feature just now starting to appear in C++ compiler
If by "just now" you mean "almost two decades ago", you'd be correct. They're not new, and all decent compilers have a mature implementation in place.

Seems like a google search for namespaces and a failure to check the dates, similar to what he did here actually!

Thanks Narue!! :)

then whats the difference between using namespace and #include? doesnt #include also tells c++ to go look in this header file for this function or statement...

then whats the difference between using namespace and #include?

The two are unrelated. Any confusion you might have about them could stem from thinking that they're somehow comparable in functionality.

then whats the difference between using namespace and #include? doesnt #include also tells c++ to go look in this header file for this function or statement...Quoted Text Here

No, it doesn't tell the compiler to go look anywhere - the header file is simply "included" in the source file. The header files contain the declarations for the functions that may be used, but the actual functions themselves are still back in the library and will be linked in by the linker at link time, and the name of those functions start with std (or are qualified with).

Namespaces are like different towns or cities. You can have a street called "Main Street" in many different towns, and each "Main Street" is qualified by it's own town. For example,
Detroit::Main Street.
Dallas::Main Street
Bakersfield::Main Street

If you are living in Bakersfield, then you can refer to Main Street by itself, because you are already in Bakersfield so that Main Street is assumed. But if you are in Bakersfield and you are referring to the Main Street in Detroit, then you must use the namespace "Detroit" to indicate that: Detroit::Main Street.

Just because you include a library ( like <cmath> ) doesn't change the fact that the functions in that library are in a different "town" and need to have their names quailified by the "town" name.

then whats the difference between using namespace and #include? doesnt #include also tells c++ to go look in this header file for this function or statement...

Not exactly. A #include line is very little more than a copy-paste instruction. It tells the compiler to look for the given file (header) and copy its content in place of the #include line. And, including a header file does not tell the compiler that any particular function or class or object will be found there. Basically, everything you include, plus every other header those included headers include makes up the sum-total of all the code that the compiler sees at one time (called a "translation unit") when compiling your code. That translation unit can be very large and contain code in many different namespaces (and sub-namespaces). To speed up the process of looking up functions and classes, and also to avoid name-clashes between them, things are split up into namespaces and addressed with their namespace (as std::cout). And the using namespace std; statement or the using std::cout; statement are just ways to momentarily avoid the full namespace qualification for every name.

Actually, the header files only contain the declarations of the functions...
for example....
the declaration of cout is in iostream as "extern ostream cout" but its only declaration and the compiler will not know where its actual definition lies(i.e how this function should work) and it is defined in a package called namespace std;
this is why we use namespace in every program

and it is defined in a package called namespace std;
this is why we use namespace in every program

Everything was fine up to the quoted part. There's no "package" called namespace std, it's a logical grouping of all names in the standard library, not some physical module that defines things. In fact, you'll find that in the standard headers the declarations are also wrapped in the std namespace.

Can this code 'using namespace std' be use in keil uVision4?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.