Your very first
C++ Program
All the example programs in this book have been tested in
Turbo C++ compiler (the programs were also tested in Visual C++ 6.0). They
should run on other C++ compilers as well.
Let us start with a basic C++ program. This will give you an
idea about the general structure of a C++ program. Let’s write a program in
which the user can enter a character and the program will display the character
that was typed:
Explanation for the above program:
- // My first C++ program
: The first line in the above program forms the
‘comments’. In C++, any line that is typed after the two slashes ( // ) is
known as comments. The compiler does not read comments. Comments can be used
anywhere in a program and you can write anything that you want in the
comments. Comments are useful when a programmer (or someone else) reads the
source code in the future. Proper comments will make it easier for anyone to
understand the program logic. When you write a very long program, you will
appreciate the use of comments.
-
# include <iostream.h
: Next comes the pre-processor directive. Preprocessor directives are
instructions meant specifically for the preprocessor. Before compiling a
program, the preprocessor will scan the program and perform text substitutions
in the source code. The preprocessor directive usually comes at the beginning
of the program (all directives for the preprocessor start with the # symbol).
*.h files are known as header files. There are many other header files like:
conio.h, graphics.h etc. There are certain operations that you will frequently
use in a C++ program. Defining these operations in each program is a
cumbersome process and leads to more lines of code. Thus these standard
operations have been written in header files which a programmer can include in
his coding (rather than coding everything again). We can also create our own
header files (we’ll look at this later). When the preprocessor encounters
#include <iostream.h>, it will physically include the iostream header file in
your source code (i.e. the entire file called iostream.h file will be pasted
in your source code). This is one of the standard header files which you’ll
use in almost all of your C++ programs.
- int main( )
: Every C++ program has to have one ‘main’ function.
Functions will be explained later. Remember that the compiler will execute
whatever comes within the ‘main’ function. Hence the instructions that have to
be executed should be written within the ‘main’ function. Just as the name
implies, ‘main’ is the main part of the C++ program and this is the entry
point for a C++ program.
- {
: Functions are defined within a
block of code. Everything within the opening and closing braces is considered
a block of code. ‘main’ is a function and hence we define this function within
braces. This is as good as telling the compiler, “The ‘main’ function starts
here”.
- char letter;
: ‘letter’ is the name of a variable. Instead of the name
‘letter’ we could also use any other name. ‘char’ defines ‘letter’ as a
character variable. Therefore, the variable ‘letter’ will accept the value of
only one character.
- cout << "Enter any letter" ;
: This is
followed by the << operator (known as the insertion operator). Following this
operator, anything typed within double quotes will be displayed on the screen.
Remember that cout and << go together. ‘cout’ is known to the compiler already
(since it is defined in the iostream header file which has been included). So
when the compiler comes across cout<<, it knows what to do.
- cin >>letter;
: cin is the opposite of cout. cin>> is used to obtain
the value for a variable from the user. (>> is known as the extraction
operator). Input is usually obtained from the keyboard.
- return 0;
: This statement tells the compiler that the ‘main’ function
returns zero to the operating system (return values will be discussed in the
chapter on functions).
- }
: The closing brace is used to tell the compiler that ‘this is the
end of the function’. In our case, it is the end of the ‘main’ function and
this indicates the end of the program as well.
You might have noticed in the above program that every
statement is terminated with a semi-colon (;). That is exactly the use of the
semi-colon. It is used to tell the compiler that one instruction line is over.
If you don’t put semi-colons in your program, you will get errors while
compiling.
A variable is used for temporary storage of some data in a
program. In the above example, ‘letter’ was a variable. Every variable belongs
to a particular type (referred to as data type). The different data types in C++
are discussed later. In the above program:
char letter;
declares ‘letter’ as a character variable (a character is one
of the basic data types in C++). When the compiler encounters this statement, it
will allocate some memory space for this variable (i.e. any value which is
assigned to the variable ‘letter’ will be stored in this allocated memory
location). When the required memory space has been allocated we say that the
variable has been defined (in this case a single statement will declare and
define the variable ‘letter’).
Some points to remember:
When we want to display something on the screen we will code
it as:
cout<<variable-name;
When we want to obtain a value for a variable from the user
we will say:
cin>>variable-name;
Beware of the direction of the >> and << operators. In the
statement
cout<<variable;
information flows from the variable into ‘cout’ (which means
it goes to the monitor display). When we say
cin>>variable;
information (the value of the variable) flows from ‘cin’
(which would be the keyboard) into the variable (i.e. the value is stored in the
variable). Information will flow in the direction of the arrows (<< or >>).
‘cout’ is linked to the standard display device (i.e. the monitor) while ‘cin’
is linked to the standard input device (i.e. the keyboard). Hence, you can’t use
cout>>variable;
This will cause an error while compiling. ‘cout’ and ‘cin’
are already pre-defined and so you can use them directly in your programs. 'iostream.h’
is a header file that is used to perform basic input and output operation (or
general I/O like using ‘cin’ and ‘cout’).
Remember: C++ is a case-sensitive language, which means
that the compiler will not consider ‘letter’, ‘LETTER’ and ‘Letter’ as the same.
Go back
to the Contents Page
Copyright © 2004 Sethu Subramanian All rights
reserved.