Writing a first program in C++

Along the lines of the popular series of books, let us entitle this series of posts as “C++ for Dummies”. A thing to note: The presence or absence of the verb form “is” can significantly alter the intended meaning of a phrase. “C++ for Dummies” gives the impression of an author expounding an esoteric subject in simple language to suit the understanding of laymen. On the other hand, “C++ is for dummies” represents a not-altogether favourable judgement on the language itself, and by extension, on those who’ve attained any level of mastery in it (a group from which the present author is thankfully and rightfully excluded).

A program is a set of instructions (and related information) that you would like your computer to execute. You basically need two things in a program. One, sets of instructions, and two, data which those sets of instructions will manipulate. Those sets of instructions are called “functions”.

Every C++ program has a function called the “main” function. As the name suggests, it is indeed the MAIN function in your program.

A textbook example of a typical first C++ program will show you how you can display some stupid/profound message on your computer screen.


From what we’ve seen thus far, we know that this simple program (to display our message) will have the “main” function. Let’s say that this program contains ONLY one function – the “main” function.

Now, inside this “main” function, we will instruct the computer to print out our cherished message on the screen.

The instruction for printing anything on the computer screen is “cout” (pronounced as “see out”).

We write the “cout” instruction generally as:

cout << “My message” ;

A few things to note here:

1. To print anything on the screen, we use the keyword “cout”. (A keyword basically means that the computer understands what it means; so we can directly use those words in our program.)

2. After typing “cout”, we type 2 less-than signs. i.e. cout <<

(Note that we may or may not put a blank space between cout and <<.)

3. Then inside inverted commas (that is, ” and “), we type the message we want to print.

4. After writing the message, we type a semi-colon (that is, a “;” ).

Now that we understand something about printing stuff on the computer screen, let us try to do so.

Here is a (part of a) C++ program.

void main(void)


cout << “Long live the Revolution!!!”;


A few more comments on the above code fragment.

1. Before the name of our function (that is, main), we have written the word “void”. If you already know as to why it is written, well and good. And if you don’t, equally good — there is no need to know the reason for that as of now.

2. After the name of the function, we put a left parenthesis and a right parenthesis, within which we write the word “void”. (that is, (void) ).

3. The set of instructions that we write inside the function is placed within curly braces (that is, { and } ).

We earlier said that the computer recognizes the meaning of “cout”. That wasn’t actually true. In order to make the computer understand the meaning of “cout”, we need to add a line before starting our “main” function —

#include <iostream.h>

As of now, it is enough to learn verbatim this statement, and if you wish, know that iostream stands for “input output stream”.

So, the complete program is:

(there might be doubts that come to your mind as regards this program. It’s always good to have doubts.)

#include <iostream.h>

void main(void)


cout << “Long live the Revolution!!!”;



So, here you’ve taken a small step (or, a big leap, depending on your outlook towards life in general) towards learning to program in C++.

As for the relevance of this program, I would only like to say that the message printed when this program runs is probably far more profound and important that the actual learning of the art of programming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: