cout is defined in std namespace and we also include iostream to use cout...i am confused ,please clarify this.where
1)cout is defined
2)where it is declared.
and if we are including iostream ,then what is the need of STD::COUT

2 Years
Discussion Span
Last Post by vmanes
Featured Replies
  • When you `#include iostream`, you get the object `std::cout` That is, you get the object `cout`, which is in the std namespace. You can use an object in the std namespace either directly: `std::cout << "Something";` or by stating that you want to use that particular object like this: using … Read More


When you #include iostream, you get the object


That is, you get the object cout, which is in the std namespace. You can use an object in the std namespace either directly:

std::cout << "Something";

or by stating that you want to use that particular object like this:

using std::cout;
cout << "Something";

or that you want to use everything from std namespace (this one is generally a bad idea, and if you do it in a header file someone will be very angry)

using namespace std;
cout << "Something";

The object named cout that is part of the namespace std is declared in iostream (or something included from iostream, or maybe in iostream it's simply declared with extern), and that declaration contains the information that cout is part of the std namesapce.

cout will be defined in one of the files that came with your compiler (for example, in the libstdc++), but exactly where and how is up to whoever wrote it. The standard doesn't enforce where it is defined.

Edited by Moschops


@moschops thanks a lot ! can i see where it is written?and whats in std namespace?


If you don't know what namespaces are then you need to read a good tutorial because namespaces are fundamental to c++ language. Namespaces were invented to keep from getting name conflicts from one file to the next, that was a big problem in C language. For example, file A.h might declare a struct named foo, and file B.h might declare another structure with the same name. If both files are included in the same *.cpp file then there is a name clash. Namespace helps prevent that problem.

All (or most) standard C++ header files are under the namespace called "std". So when you write using namespace std; you are telling the compiler to use everything in that namespace regardess of where it comes from. In small pograms like you might write in school that is not a big problem, but in large professional c++ program it can become a problem. There are a couple alternate ways of coding it, such as using std::cin; or just writing std::cin << "Hello World\n";


Where is it written? If you follow up the chain of library files, you'll come to:
Which has the line:
#define _STD_BEGIN namespace std{

Most every library file, after its #include statements, leads off with:

(at least that's where it exists in MS Visual C++)

Edited by vmanes


thnks vmanes.
my teacher told following format for a namespace--

namespace abc
int a;

but in yvals.h all i could find is

#define _STD_BEGIN  namespace std {
  #define _STD_END      }

where is cout ?
sorry i just started c++ and got confused with namespace std and iostream.


cout is defined in <iostream> header file. As a beginner, don't try to read iostream header file -- it's a big mess. Just be aware that cin and cout are declared in iostream. If you want to do file io then use fstream, ifstream and ofstream classes. Trying to follow the header files can ruin your brain.

Edited by Ancient Dragon


As a beginner, the easiest thing is to just always put
using namespace std;
after your program's #include statements.

Like Nike says, "Just do it."

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.