©1994-2003 Kevin Boone
Home     Section index     K-Zone home C++ tutorial (under construction)

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
types
contents
cin
C++ tutorial: cout


Use 'cout' for simple output
Probably the simplest form of output that a computer program can produce is to write plain text onto the screen. That's what 'cout' is for (I pronounce this like 'c out'; I think it stands for 'console out'). For example, in the programs welcome1.cpp and welcome2.cpp you should remember the following line:

cout << "Welcome to BIS4221:\n";

'cout <<' is an instruction to write the following thing to the screen. In this example, the 'thing' to be written is a line of text: "Welcome to BIS4221:\n". The quotation marks at the start and end of the line indicate that the stuff inside is to be treated as a single 'lump' of text (technically it is a character array, of which more later). Everything inside the quotes gets printed to the display, except the part '\n'. This does not appear in the output. Can you work out what this does? If you have a C++ compiler, try removing the '\n' and see what happens. If you don't, the answer is at the bottom of this page.

You can 'cout' several things at once
As you will see, you can use 'cout' to output other types of data, not just text. You can ouput several things on one line, like this:

cout << something1 << something2;

where the 'somethings' are the data you want to display.

'cout' is an object
Technically, cout is a 'object of class ostream'. An object is a self-contained entity consisting of functions and data. We will not be introcing objects and classes until later, but it is as well to get the terminology right at the beginning. All 'objects of class ostream' are concerned with outputting formatted data; 'cout' in particular is associated with the display. You can define related objects to ouput data to files, or to the network. So what does '<<' mean? One way of interpreting it is as a 'send to' instruction. In other words,

cout << "Welcome to BIS4221:\n";
means 'send a piece of text to the cout object'. Personally I think this is a slightly ugly notation, as '<<' has another, totally different meaning in C++. One of the powerful features of C++ is the ability for the programmer to re-define the meaning of symbols; in this case the designers of C++ thought that '<<' was a good symbol for 'send to'. I prefer for symbols to mean similar things in all contexts, as do the designers of Java, as this facility was removed from the Java language. In any event, '<<' in the context of 'cout' means 'send to'. What do you think '>>' might mean? All will be revealed in due course!

PS:
'\n' means 'start a new line'. Other similar symbols are available, e.g., '\t' means 'indent one tab space'.