im curious as to if anyone knows the internals of how cout works?

for example the step by step internals of what happens when executing the following statement in c++

cout << "hello world" << endl;

-thx

Recommended Answers

All 8 Replies

Yes, I know. And it's easily the most complex part of the standard C++ library. How many hundreds of pages do you expect for a complete description of everything that's going on?

1st: "cout" prepare for a text output 2nd: "<<" seperate function from output 3rd: "hello world" output text 4th: "<<" same as second 5th: "endl" create a new line after output 6th: ";" end of line

Yes, I know. And it's easily the most complex part of the standard C++ library. How many hundreds of pages do you expect for a complete description of everything that's going on?

just curious, why would you take the trouble to learn that? For fun?

Have you had a look at the header that defines it? That's probably a good place to start :0) you can also find details about it on cplusplus.com.

@Thecoolman5: I'd also recommend that you go and look it up too, since your answer indicates that you possibly don't understand how it works either.

Well, I am a beginner in coding. I knew how cin worked so I just kind of said the opposite for cout. And I am guessing the cout declaration is in the iostream header file.

As a further answer, a good implementation to study can be found here.

just curious, why would you take the trouble to learn that? For fun?

You say that like it's not fun. ;) I simply wanted to understand how stuff worked under the hood and went a bit deeper than your average Joe.

Well, I am a beginner in coding. I knew how cin worked so I just kind of said the opposite for cout. And I am guessing the cout declaration is in the iostream header file.

Yeah, I think it's going to to be in there some where. I think cout derived from ostream, which might be derived from something like basic_stream, but I can't remember exactly.

Have fun :0)

The architecture is fairly straightforward.

The stream class (in case of std::cout , std::ostream ) is responsible for parsing and formatting the sequence of characters required for output.

The stream buffer object associated with the stream provides an abstract connection to some external device; the stream buffer is responsible for transport of characters to ( from for input) this external device, and buffering of these characters in an internal buffer.

For example in,

#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    int i = 1234 ;
    double d = 123456.789 ;
    bool flag = i > 100 ;
    std::cout << std::fixed << std::setprecision(2) << std::boolalpha ;

    std::cout << "to stdout: " << i << ' ' << d << ' ' << flag << '\n' << std::flush ;

    std::filebuf stmbuf ;
    stmbuf.open( "out.txt", std::ios::out ) ;
    std::streambuf* old_buf = std::cout.rdbuf( &stmbuf ) ;
    std::cout << "to file: " << i << ' ' << d << ' ' << flag << '\n' << std::flush ;

    std::cout.rdbuf( old_buf ) ;
    std::cout << "back to stdout: " << i << ' ' << d << ' ' << flag << '\n' ;
}

std::cout formats the output (converts objects int, double etc. to a sequence of chars). The streambuf sends these chars to the external device as required (eg. a std::filebuf sends it to a file).

See: http://www.angelikalanger.com/IOStreams/Excerpt/excerpt.htm
And for more detailed information on the architecture, read the book.

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.