When I wrote something like

cout << "Every age has a language of its own";
It's supposed that the sentence between the two quotation marks will not be targeted directly to the screen, but will be stored somewhere first before directed to the screen.

What is that place in which the sentence will be stored?

The same thing is done in a reverse way with the cin so the question is,

Is it the same storing place is used to store what is enetered by the user on the screen before assiging it to a variable of whatever was in the right hand side of the cin?

What does it mean that the header file iostream contains the declarations that are needed by the cout identifier and the << operator?. What does it mean by the declarations? If with an illustrative example of this would be the best.

What is the content of the namespace? what it means that certain names are recognized in namespace std including cout is declared within it! is not cout declaration exists in the iostream header file?

Recommended Answers

All 11 Replies

I may be wrong, and I know I am not saying it all, but as I understand it the namespace in which cin/cout live (std::) is full of stuff... literally anything that is standard c++ should be in there (thats why they call it std, its short for standard, not sexually transmitted diseases!). cin and cout are actually just global variables that sit inside the std namespace (so I guess they are only sorta global). Their types are istream and ostream respectively. Now the question is what do istream and ostream do? istreams and ostreams (input and output streams) are defined in iostream.h (which incidentally also defines iostreams which combine the uses of istreams and ostreams) and they deal with streaming data. Data streaming is when you send data bit by bit rather than all at once. When you read this your eyes stream the information to your brain, then if you need to remember it, the memory part of your brain streams it to the thinking part of your brain. Similarly iostreams let you stream data into or out of a stream. A stream can be pretty much anything (I don't actually know off-hand how to define a stream, but it shouldnt be too hard to look up). In the case of cin/cout the streams are the console. cout streams "Every age has a language of its own" to the console, which does some magic graphics stuff to show the user the text. Similarly cin asks the console to stream it data, and the console magically waits for you to press ENTER while recording what you do so as to send it to cin. Inside cin and cout this data is stored somehow (probably as an array) so that it can be sent to (or retrieved from) the variables you send it.

An interesting thing you can do then is redirect cin/cout. So by including fstream.h (f is for file here) you can say "cout... could you read from this text file instead of from the console" (again I can't remember the exact syntax for this). You can see this in action in many IDEs if you create a graphics program and try to use cout... since there is no console (Can you imagine if all graphics programs created a console! that would be utter insanity!) it creates stdin.txt and stdout.txt to use as cin/cout instead! (NB: it also creates stderr for cerr).

All that you should really worry about however is how to use cout... inside they do some fancy code-work (probably using some assembly interrupts, or c io functions?) to make your data show up on the screen. As long as you know that you either need to say "using namespace std;" or "std::" to access cin/cout you shouldn't have any issues. If you want to learn more about the subject I would suggest looking at http://cplusplus.com/ to read about standard c++. Also if you don't understand what namespaces are you should probably read a book or a tutorial to teach you more about c++. I personally used learncpp.com.

Hope this helped!

It's supposed that the sentence between the two quotation marks will not be targeted directly to the screen, but will be stored somewhere first before directed to the screen.

I'm guessing you mean the buffering mechanism. For performance reasons, because you don't want to communicate with devices (a typically slow operation) for every single character, output streams will implement a buffer that collects a certain number of characters before writing them all to the device en masse. Input streams do the same thing, except in reverse. An input stream will collect as much data as possible from the device and store it in a buffer, then dole out characters to your program as requested. This way the device is invoked less often without affecting functionality.

What is that place in which the sentence will be stored?

It depends on the implementation of the streambuf contained by the stream object. But it could be as simple as an array of characters in the streambuf object, or no buffer at all in cases where the stream is unbuffered.

What does it mean that the header file iostream contains the declarations that are needed by the cout identifier and the << operator?. What does it mean by the declarations? If with an illustrative example of this would be the best.

I can do one better. This is the iostream header in its entirety (barring variations per compiler):

#ifndef IOSTREAM
#define IOSTREAM

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;
}

#endif

All it does is declare the standard I/O objects by specifying their name and type.

What is the content of the namespace?

Assuming you mean the std namespace, everything in the standard C++ library is in it, with the exception of the deprecated C headers that end in ".h" (eg. <stdio.h>, <stdlib.h>). Those are there for backward compatibility, and they explicitly keep the C library names out of the std namespace. The new C headers that start with "c" (eg. <cstdio>, <cstdlib>) contain all of the same stuff, but it's inside the std namespace.

Is not cout declaration exists in the iostream header file! what it means that certain names are recognized in namespace std including cout is declared within it!!!!!?

Is not cout declaration exists in the iostream header file!

Um...what?

what it means that certain names are recognized in namespace std including cout is declared within it!!!!!?

I don't understand the question. Please rephrase it.

Cout is declared in the iostream header file, so what does it mean that cout is declared in the namespace std?

Sorry, my english sometimes is not good enough :). the above question in my previous post was a denunciation question. How come cout declaration be in the iostream and in the same time in the namespace std!?

Header files act like a massive copy-paste. When you say #include "header.h" it copies all the code in header.h directly into your code, replacing the #include statement. As such, you can have a namespace (which is a type of c++ construct) inside a header file. So for example:

header.h:

int thisIsGlobal;
namespace myNamespace
{
    int thisIsInMyNamespace;
};

main.cpp:

#include <iostream>
#include "header.h"
using namespace std;
using namespace myNamespace;
int main()
{
    thisIsGlobal=7;
    thisIsInMyNamespace=10;
    cout<<thisIsGlobal<<endl<<thisIsInMyNamespace;
    return 0;
}

what main.cpp is compiled like:

#include <iostream>
int thisIsGlobal;
namespace myNamespace
{
    int thisIsInMyNamespace;
};
using namespace std;
using namespace myNamespace;
int main()
{
    thisIsGlobal=7;
    thisIsInMyNamespace=10;
    cout<<thisIsGlobal<<endl<<thisIsInMyNamespace;
    return 0;
}

Cout is declared in the iostream header file, so what does it mean that cout is declared in the namespace std?

It sounds like you think namespaces and header files somehow conflict, and that's not the case. You can think of it as headers being the physical home of declarations while namespaces are a logical home. Notice in my example for <iostream> that the std namespace is defined inside the header, and the objects are defined inside the namespace. Basically when you say something like this:

namespace std {
    extern ostream cout;
}

What really happens is as if you defined a unique name somewhat like this:

extern ostream std_cout;

All namespaces do is add a layer of uniqueness to names so that the same name can be used in different namespaces. However, instead of forcing you to define names like std_cout, the language provides scaffolding to do it for you as well as simplify use of the names with things like using namespace std.

Also note that a namespace is collective, so you can define parts of it separately:

namespace std {
    extern ostream cout;
}

namespace std {
    extern istream cin;
}

And the compiler will combine everything so that the result is as if you defined everything in a single namespace definition:

namespace std {
    extern ostream cout;
    extern istream cin;
}

That's why I could just declare the stream objects in the namespace definition for <iostream>, and it doesn't break similar namespace definitions in the other standard headers.

std namespace is defined inside the header

"deceptikon" I thought that header files are reside in the namespaces and not vice versa.

std namespace is defined inside the header

Nope, that's not how it works. The std namespace isn't "defined" anywhere. All the namespace std {...} stuff is saying is that the stuff contained in the brackets belongs to the std namespace.

"deceptikon" I thought that header files are reside in the namespaces and not vice versa.

Header files and namespaces are two completely seperate concepts. One doesn't "reside" in another. Header files are for including certain information that is required to compile whatever you are compiling. Usually things like type definitions, functions prototypes, and extern declarations go inside of header files. Namespaces are mostly just there to prevent naming conflicts.

The std namespace isn't "defined" anywhere. All the namespace std {...} stuff is saying is that the stuff contained in the brackets belongs to the std namespace.

Splitting hairs. Wouldn't specifying what "belongs to" the namespace also be considered "defining" the namespace? The only confusing part might be that a namespace is allowed to have multiple definitions that collectively define it.

One doesn't "reside" in another.

Namespaces have to be defined somewhere, and once again it's splitting hairs to say that "resides in" doesn't equate closely enough to "defined in" to have identical meaning.

AmrMohammed may also be misunderstanding what a header is and does. It's just another text file[1] that contains certain things (declarations, and such) through convention.

[1] A compiler may choose to store the headers it provides in another way, of course.

Wouldn't specifying what "belongs to" the namespace also be considered "defining" the namespace?

I was more trying to clarify that there isn't one single place where a namespace is "defined" like there is for a variable declaration or a function definition. Using the word "define" could stir up a little bit of confusion here because let's say instead of <iostream>, we only included <string>. Suddenly the std namespace has a different "definition"! That's why I think using the term "belongs to" is much more clear.

Namespaces have to be defined somewhere, and once again it's splitting hairs to say that "resides in" doesn't equate closely enough to "defined in" to have identical meaning.

I'm not making a distinction between "defined in" and "resides in" in this case, I was simply stating that it's not required that a namespace has to be in a header, which is true. You can put namespaces in your cpp files if you want.

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.