|
|
|
|
|
|
|
Imagine that you are the C++ compiler. You are presented with the following program. You are to check it for syntax errors and, if there are no syntax errors, you are to translate it into machine language code. |
|
|
|
|
|
|
|
|
//************************************
// This program prints Happy Birthday
//************************************
int main()
{
cout < Happy Birthday < endl;
return 0;
} |
|
|
|
|
|
|
|
|
You, the compiler, recognize the identifier int as a C++ reserved word and the identifier main as the name of a required function. But what about the identifiers cout and endl? The programmer has not declared them as variables or named constants, and they are not reserved words. You have no choice but to issue an error message and give up. |
|
|
|
|
|
|
|
|
The way to fix this program is to insert a line near the top that says |
|
|
|
|
|
|
|
|
just as we did in the FreezeBoil program (as well as in the sample program at the beginning of this chapter and the Payroll program of Chapter 1). |
|
|
|
|
|
|
|
|
The line says to insert the contents of a file named iostream.h into the program. This file contains declarations of cout, endl, and other items needed to perform stream input and output. The #include line is not handled by the C++ compiler but by a program known as the preprocessor. |
|
|
|
|
|
|
|
|
The preprocessor concept is fundamental to C++. The preprocessor is a program that acts as a filter during the compilation phase. Your source program passes through the preprocessor on its way to the compiler (see Figure 2.3). |
|
|
|
|
|
|
|
|
A line beginning with a pound sign (#) is not considered to be a C++ language statement (and thus is not terminated by a semicolon). It is called a preprocessor directive. The preprocessor expands an #include directive by physically inserting the contents of the named file into your source program. Files that appear in an #include directive usually have a file name ending in .h, meaning header file. Header files contain constant, variable, and function declarations needed by a program. |
|
|
|
|
|