Section 5.5 Preprocessor directives | ||
Preprocessor directives are orders that we include within the code of our programs that are not instructions for the program itself but for the preprocessor. The preprocessor is executed automatically by the compiler when we compile a program in C++ and is in charge of making the first verifications and digestions of the program's code.
All these directives must be specified in a single line of code and they do not have to include an ending semicolon ;.
#define name valueIts function is to define a macro called name that whenever it is found in some point of the code is replaced by value. For example:
#define MAX_WIDTH 100It defines two strings to store up to 100 characters.
char str1[MAX_WIDTH];
char str2[MAX_WIDTH];
#define can also be used to generate macro functions:
#define getmax(a,b) a>b?a:bafter the execution of this code y would contain 5.
int x=5, y;
y = getmax(x,2);
#define MAX_WIDTH 100
char str1[MAX_WIDTH];
#undef MAX_WIDTH
#define MAX_WIDTH 200
char str2[MAX_WIDTH];
#ifdef allows that a section of a program is compiled only if the defined constant that is specified as the parameter has been defined, independently of its value. Its operation is:
#ifdef nameFor example:
// code here
#endif
#ifdef MAX_WIDTHIn this case, the line char str[MAX_WIDTH]; is only considered by the compiler if the defined constant MAX_WIDTH has been previously defined, independently of its value. If it has not been defined, that line will not be included in the program.
char str[MAX_WIDTH];
#endif
#ifndef serves for the opposite: the code between the #ifndef directive and the #endif directive is only compiled if the constant name that is specified has not been defined previously. For example:
#ifndef MAX_WIDTHIn this case, if when arriving at this piece of code the defined constant MAX_WIDTH has not yet been defined it would be defined with a value of 100. If it already existed it would maintain the value that it had (because the #define statement won't be executed).
#define MAX_WIDTH 100
#endif
char str[MAX_WIDTH];
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. The condition can only serve to evaluate constant expressions. For example:
#if MAX_WIDTH>200Notice how the structure of chained directives #if, #elsif and #else finishes with #endif.
#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];
The #line directive allows us to control both things, the line numbers within the code files as well as the file name that we want to appear when an error takes place. Its form is the following one:
#line number "filename"Where number is the new line number that will be assigned to the next code line. The line number of successive lines will be increased one by one from this.
filename is an 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. For example:
#line 1 "assigning variable"This code will generate an error that will be shown as error in file "assigning variable", line 1.
int a?;
#ifndef __cplusplusThis example aborts the compilation process if the defined constant __cplusplus is not defined.
#error A C++ compiler is required
#endif
#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 the file is specified between quotes, the file is looked for in the same directory that includes the file containing the directive. In case that it is not there, the compiler looks for the file in the default directories where it is configured to look for the standard header files.
#include <file>
If the file name is enclosed between angle-brackets <> the file is looked for directly where the compiler is configured to look for the standard header files.
© The C++ Resources Network, 2000-2003 - All rights reserved |
Previous: 5-4. Advances classes type casting. |
index |
Next: 6-1. Input/Output with files. |