Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C++

Learn step by step



Preprocessor Directives in C++

C++ preprocessor is collection of special statements, called directives, that are executed at the begenning of the compilation process.

The preprocessor is usually an integral part of our compiler. It's a process that is executed by the compiler before our C++ program code is compiled into machine instructions. The job of preprocessor is to prepare our source code, proper for compile phase, according to the instructions that we have included in the source files. These instructions are called as preprocessor directives.

All the preprocessor directives begin with the symbol #, so they are easily distinguishable from C++ language statements.

Below given is the complete set of preprocessor directives:


Directive Function
#include Supports header file inclusions
#if, #else, #elif, #endif, #if defined (or #ifdef), #if !defined (or #ifndef) Enables conditional compilation
#define, #undef Enable symbol and macro substitution
#line Allows redefinition of current line and filename
#error Produces compile-time error message
#paragma Offers machine-specific features while retaining overall C++ compatibility
#include

This directive has also been used assiduously in other sections of this tutorial. When the preprocessor finds an #include directive it replaces it by the whole content of the specified file. There are 2 ways to specify a file to be included:

#include "file"
#include <file> }

The only difference between both expressions is the directories in which the compiler is going to look for the file. In the first case where file is specified between quotes, file is looked for in the same directory that includes the file containing the directive. In a case that it is not there, compiler looks for the file in the default directories where it is configured to look for the standard header files.

If the file name is enclosed between the angle-brackets(less than and greater than sign) <> the file is looked for directly where the compiler is configured to look for the standard header files.

#ifdef, #ifndef, #if, #endif, #else and #elif

These directives allow to discard part of code of a program if a certain condition is not fulfilled.

#ifdef allows that a section of a program is compiled only if the defined constant that is specified as parameter is been defined independently of its value.

#ifdef name
// code here
#endif

For example:

#ifdef MAX_WIDTH
char str[MAX_WIDTH];
#endif

In this case, the line char str[MAX_WIDTH]; is only considered by the compiler if the defined constantMAX_WIDTH has been previously defined freely of its value. If it has not been defined, then that line will not be included in program.

#ifndef serves for the opposite: Code between the #ifndef directive and the #endif directive is only compiled if the constant name that is specified has not been defined previously.

#ifndef MAX_WIDTH
#define MAX_WIDTH 100
#endif
char str[MAX_WIDTH];

In this case, if when arriving at this piece of code the defined constantMAX_WIDTH has not yet been defined it would be defined with a 100 value. If it already existed it would maintain the value that it had (because the #define statement won't be executed).

The #if, #else and #elif (elif = else if) directives serve so that the portion of code that follows is compiled only if the specified condition is met. Condition can only serve to evaluate constant expressions.

#if MAX_WIDTH>200
#undef MAX_WIDTH
#define MAX_WIDTH 200

#elsif MAX_WIDTH<50
#undef MAX_WIDTH
#define MAX_WIDTH 50

#else
#undef MAX_WIDTH
#define MAX_WIDTH 100
#endif

char str[MAX_WIDTH];

Notice that how the structure of chained directives #if, #elsif and #else finishes with #endif.

#define and #undef
#define

At the beginning of this tutorial we have already spoken about a preprocessor directive: #define, which serves to generate what we called defined constantants or macros and whose form is the following:

#define name value

Its function is to define a macro named 'name' that whenever it is found in some point of the code is replaced by value. For example:

#define MAX_WIDTH 100
char str1[MAX_WIDTH];
char str2[MAX_WIDTH];

It defines two strings to store upto 100 characters.

#define can also be used to generate macro functions:

#define getmax(a,b) a>b?a:b
int x=5, y;
y = getmax(x,2);

after execution of this code y would contain 5.

#undef

#undef fulfills the inverse functionality of #define. It eliminates from the list of defined constants the one that has the name passed as a parameter to #undef:

#define MAX_WIDTH 100
char str1[MAX_WIDTH];
#undef MAX_WIDTH
#define MAX_WIDTH 200
char str2[MAX_WIDTH];
#line

When we compile a program and any errors happen during the compiling process, the compiler shows the error that happened preceded by the name of the file and the line within the file where it has taken place.

The #line directive allows us to control both things, the line numbers within code files as well as the file name that we want to appear when an error takes place. Syntax:

#line number "filename"

Where number is the new line number that will be assigned to the next code line. Line number of successive lines will be increased one by one from this.

filename is a optional parameter that serves to replace the file name that will be shown in case of error from this directive until another one changes it again or the end of the file is reached.

#line 1 "assigning variable"
int a?;

This code will produce an error that will be shown as error in file "assigning variable", line 1.

#error

This directive aborts compilation process when it is found returning the error that is specified as the parameter:

#ifndef __cplusplus
#error A C++ compiler is required
#endif

This example aborts compilation process if the defined constant__cplusplus is not defined.

#pragma

This directive is used to specify diverse options to compiler. These options are specific for platform and the compiler we use. Consult the manual or the reference of your compiler for more information on the possible parameters that you can define with #pragma.



Related Videos