Take the 2-minute tour ×

My computer science professor wants us to find the declaration of cout. I've compiled a simple Hello world program using g++ and the -E parameter. Here's what my hello.cpp looks like:

#include <iostream>

using namespace std;

int main(){

  string name="";

  cout << "Good morning! What's your name?";

  cin >> name;

  cout << "Hello " << name << ".\n";

  return 0; 


My compile command:

g++ -E hello.cpp > hello.p

In hello.p, I ran a search in VIM, like so:


I see the following line:

extern ostream cout;

Is that the declaration of cout, and is cout an instance of the ostream class?


What's the wcout declaration there for? If I recall correctly the letter "w" stands for "wide", but I don't know what implication that has. What is a wcout and a wostream?

share|improve this question
I'd be willing to guess somewhere in the code that gets tacked on to your executable when you link against IOStream. –  Kaslai Mar 11 '12 at 4:16
@Aslai - I've pulled out a line from that code. I want to know if that's it. –  Moshe Mar 11 '12 at 4:18
Try this: cplusplus.com - type cout into the search box. –  Tieson T. Mar 11 '12 at 4:28
Yes, cout is an instance of ostream class. –  lalitm Mar 11 '12 at 4:44
Changed the tag back to [c++], I don't know how [c++-cli] has anything to do with this. –  Xeo Mar 11 '12 at 16:13
add comment

2 Answers

up vote 6 down vote accepted

Yes, that is indeed the declaration of std::cout, found inside the <iostream> header.

The relevant standard part can be found in §27.4.1 [iostream.objects.overview]:

Header <iostream> synopsis

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

namespace std {
  extern istream cin;
  extern ostream cout;
  extern ostream cerr;
  extern ostream clog;
  extern wistream wcin;
  extern wostream wcout;
  extern wostream wcerr;
  extern wostream wclog;

p1 The header <iostream> declares objects that associate objects with the standard C streams provided for by the functions declared in <cstdio> (27.9.2), and includes all the headers necessary to use these objects.

share|improve this answer
Thanks. Mind clarifying my edits, please? –  Moshe Mar 11 '12 at 12:43
@Moshe: wcout is just a basic_ostream specialized on wchar_t, which means UTF-16 on Windows and UTF-8 on Linux IIRC. –  Xeo Mar 11 '12 at 16:12
add comment


Yes, that is the declaration of std::cout and yes it's in instance of std::ostream. It is declared extern so that the object is only created once even if the header is included by multiple cpp files.

share|improve this answer
Don't forget the namespace std { ... } part. It's std::ostream std::cout, not ::ostream ::cout. –  moshbear Mar 11 '12 at 4:21
add comment

Your Answer


