Preprocessor Directives
Q45. Discuss about preprocessor directives.
Answer:
Preprocessor Directives
Preprocessor directives are the lines of code which are included in the source file before program code. These are considered as special instructions because they tell the preprocessor to process the source code (or) program before compilation. It always begins with a hash (#) symbol.-A preprocessor directive is not closed with a semicolon (;).
Some of the preprocessor directives are as follows,
- Source file inclusion
- Macro definitions
- Conditional inclusions
- Line control
- Error directive
1. Source File Inclusion
The source file inclusion directive is represented as #include, it replaces complete contents of header (or) files. It can be used in two ways,
(i) #include <header>
The first form includes headers such as iostream from the standard library header is enclosed within angle-brackets < >.
(ii) #include “file”
The second form uses quotes and includes the file. It tells the preprocessor to locate the file in an implementation defined manner in the current path. The compiler interprets the directive as a header inclusion when file is not found.
2. Macro Definitions
a) #define
The macros are defined by using # define preprocessor directive. The syntax of macros is as follows,
Syntax:
# define MACRO_name replacement
When the processor encounters this preprocessor directive, it replaces all the: occurrences of macro-name by replacement in the program.
Example
# define SIZE 100
int s1[S1ZE];
int s2[S1ZE];
The code after replacement of size is as follows:
int s1[100]
int s2[100]
As SIZE is replaced by 100.
The #define can also be combined with parameters.
#define macro-name (parameters) replacement
Example:
#define getmax (a, b) a> b? a:b
The above example replaces occurrence of getmax followed by two arguments by replacement expression. In addition to this, it also replaces every argument by identifies as function.
b) #undef
The macro definitions are undefined by using # undefined directive. The defined macro can be used until it is undefined.
Syntax
# undef MACRO_NAME
Example
# define SIZE 100
int sl [SIZE]
#undef SIZE
# define SIZE 200
int s2 [SIZE]
The above example generates the result as follows,
int s1[100]
int s2[200]
Here initially the SIZE is defined as 100. To change the value of the SIZE, it should be undefined and redefined again as 200
3. Conditional Inclusions
These preprocessor directives enables to include (or) discard certain part of the code when a condition is met.
The types conditional inclusions are,
(a) #ifdef
(b) #ifndef
(c) #if
a) #ifdef :
It allows the part of the program to be compiled only when the specified parameter macro is defined.
Syntax
# if def MACRO_name
Body
# endif
Example
# if def SIZE
int s1[SIZE];
# endif
The first line gets compiled only when size is defined with #define without the concern of its value. Otherwise, it will not be added to the program.
b) #ifndef
# ifndef is the opposite of # ifdef directive. It is used when macro definitions are not defined:
Syntax
ifndef MACRO
Body
# endif
Example
# ifndef SIZE
# define SIZE
# endif
int s1[SIZE]
In the above example, the macro name SIZE is not defined. So, it accepts and executes the statements.
c) #if
This directive is used to control the certain part of the code, when the program meets any condition. The # if is used either with # else or elif directives in the form of a pair. The end of # if statement is delimitted by using # end if directives.
Syntax
Syntax for # if with # else is as follows,
# if condition>
body
# endif
(or)
# if <condition>
<statement 1>
# else <statement 2>
# endif
Syntax
Syntax for # if with # elif is as follows,
# if <condition>
<statement I>
# elif <condition>
<statement 2>
..............
# elif <condition>
<statement n>
# else
<default statement>
# endif
Example
Example using # if and # else
# if SIZE > 200
# cout <<"size is greater than 200";
# else
cout <<"size is smaller than 200";
# endif
Example
Example for # if # elif
if MAX > = 100
cout <<"\n\";
# elif MAX > = 150
cout <<"\n2";
# elif MAX > = 200
cout’<<"\n3";
# else
cout <<"4";
# endif
4. Line Control
The line directive enables to control the line number and the file names when the error is generated. This helps to find the code generating error and makes the process easy.
Syntax
# line number "filename"
In the above syntax # line is the directive, number is the line number and filename is name of the file.
Example
# line 12 "assigning variable"
The above example represents that an error is generated as assigning variable in line 12.
5. Error Directive (#error)
This #error directive terminates the compilation process, when the macroname is not defined.
Syntax
#error
Example
if ndef SIZE
#error SIZE should be defined
#end if
The above example represents that the SIZE is not defined so it terminates the compilation process.