Folder101   Home Notes Quiz Assigns Info  
Forum Forum
101 Home 101
 
  C++ Level 2

Directives and the Preprocessor

The Preprocessor

#include Directive

#define Directive

Extension Work


  The Preprocessor

The preprocessor performs preliminary operations on C++ files before they are passed to the compiler. These operations include inserting source code from files, defining constants, changing your source code and other operations.

Directives in the source file tell the preprocessor to perform specific actions. There are quite a few directives the preprocessor recognizes; here are two:-

#include

#define

The first character is the # symbol, which is a signal to the preprocessor. Each time you start your compiler, the preprocessor is run. The preprocessor reads through your source code, looking for lines that begin with the pound symbol (#), and acts on those lines before the compiler runs.


  #include Directive

The preprocessor is able to add and remove code from your source file using the #include directive. This is an instruction that tells the preprocessor to include code from a particular file. As an example, consider the HelloWorld program which uses the following directive:-

#include <iostream.h>

This tells the preprocessor to include code from the file iostream.h (Input-Output-Stream). This file contains declarations for functions that the program needs to use, such as the cout function. The cout function helps you to write to the screen.

The effect of the directive is as if you had typed in the code inside iostream.h into your own file. In other words the #include directive says, "What follows is a filename; find that file and read it in right here."

Of course, the preprocessor has to find the file first. This is what the angle brackets around the filename are for. They tell the preprocessor to look in the usual folder that holds all the .H files for your compiler. Normally this will be the C:\Program Files\Microsoft Visual Studio\VC98\Include folder.


  #define Directive

The #define directive is used to to define a constant in your program. Here is an example:-

#define PI 3.141

Wherever the constant name PI appears in your source file, the preprocessor replaces it by its value. When the compiler runs, the compiler will only see the value 3.141 in your code, not PI. Every PI is just replaced by its value.

We will be covering constants in more detail later on in the course.


  Extension Work

Carry out the following exercises:-

  1. Open your helloWorld program. Then locate the following line:-

    #include <iostream.h>

    Remove the whole of the line from your code. Try and compile your program by selecting Compile or Build HelloWorld.exe from the Build menu. What error message do you get? You should get:-

    'cout' : undeclared identifier

    This is because the line you removed told VC++ to include the a file called iostream.h that comes with VC++. You need this file to be able to use the cout function which sends output to the command prompt console. We shall discuss this in a later lesson. Make sure you type the line back in.

Thats it!
Now lets look at another look at Hello World
.