< previous page page_20 next page >

Page 20
LISTING 2.1HELLO.CPP DEMONSTRATES THE PARTS OF A C++ PROGRAM

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1: #include <iostream.h>
2:
3: int main()
4: {
5:    cout << Hello World!\n;
6:    return 0;
7: }

Output:
Hello World!
Analysis: On line 1 the file IOSTREAM.H is included in the file. As far as the compiler is concerned, it is as if you typed the entire contents of the file IOSTREAM.H right into the top of HELLO.CPP.
Examining the #include, Character by Character.
New Term: The first character is the # symbol, which is a signal to the preprocessor. What the heck is the preprocessor?
When you run your compiler, it first calls another programthe preprocessor. You don't have to invoke the preprocessor directly; it is called automatically each time you run the compiler.
The job of the preprocessor is to read through your source code looking for lines that begin with the pound symbol (#). Each time it finds a line beginning with the pound symbol, the preprocessor modifies the code. It is the modified code that is given to the compiler.
The term include is a preprocessor instruction that says, What follows is a filename. Find that file and read it in right here. The angle brackets around the filename tell the preprocessor to Look in all the usual places for this file. If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file Iostream.H in the directory that holds all the H files for your compiler. These files are called h files or include files because they are included in source code files and end with the extension .h. The file Iostream.H (Input-Output-STREAM) is used by cout, which assists with writing to the screen.
The effect of line 1 is to include the file Iostream.H into this program as if you had typed it in yourself. By the time the compiler sees this file, the included file is right there, and the compiler is none the wiser.

 
< previous page page_20 next page >

If you like this book, buy it!