Click here to Skip to main content
15,894,210 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how the cout statement works in c++? and how the output is displayed to the screen with it?
Posted

read this[^]
 
Share this answer
 
That's a long problem that in involve a large number of concepts, some related to
the language standard library and some related to the platform specific implementation.

cout


Firsts of all, std::cout is a global variable, declared in
<iostream> and defined somewhere in the c++ runtime support library.

It's type is an internal class, derived from std::ostream, that embed a member
derived from std::streambuff and a member of type std::locale.

when you write
std::cout << 45;


the function
std::ostream& operator<<(std::ostream&, const int&)
is called.

There is a variant of this function for any of the built-in types plus some other
for particular types like const char* or const std::string etc.

Depending on the variant of the function (that is: depending on the parameter type)
- the stream associated locale is found and the proper type-to-text conversion facet is found.
(for an integer, this is the std::num_put facet class)
- the put function is called, converting the number into its representative text
to be stored into the stream associated streambuff.

When the buffer is full (or when the buffer sync function is called, for example
by means of the std::endl manipulator) its content is written to thje associated file.

For cout, the buffer's associated file is the "CON:" symbolic file, that represent
what for the operating system represent the standard output.

The console


At this point, C++ ended its job, and a text is written into the CON file the operating
system mapped to an internal process that dispatches the content to the "application console".

This is just a particular window whose window-procedure consist in panting a window with
the text present on a buffer that is a matrix of characters and related attributes that is
filled-in with the characters retrieved by the CON file buffer or by appropriately parsing them
(for example \n moves the cursor to the next line, \b moves it back etc.) every time that file is
notified to be changed.

Low level API



The "painting" action consist in retrieving the characters and give them to the
TextOut API (or similar) that, using the selected font, converts the
character sequence in their respective images, that are copied in the proper position
into the "device context", that -fro the screen- is a sequence of bytes located on
a memory mapped page that represent the raster of the graphic adapter, whose processor
is n charge to transform the image bits into the analog signal that drives the monitor.

This last point has no difference in respect to display whatever else for whatever other application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900