Introduction to C Preprocessor Directives
The preprocessor is a program that processes the source program before it is passed on to the compiler. C preprocessor provides the facility to programmers to make the program more efficient (for understanding and modification of program). The preprocessor directive usually written at the beginning of the C program. These directives are preceded with the #(hash) symbol and there is no semicolon at the end. This is not necessary to write the preprocessor directives at the beginning of the program. These can be written anywhere in the program but they are generally written before the main () or other functions.
The #define Directive
#define directive is used to define the macro name and macro expansion. There is no semicolon at the end of the statement and a space is necessary between the macro name and macro expression. Macro expansion is terminated by only new line. The syntax of #define directive is as follows:
#define macro_name macro_expression
OR
#define identifier <substitute text>
OR
#define identifier (argument1, argument2,........., argument N) text
In the compilation process of the C program, first the C preprocessor searches for the macro name in the entire program and replaces the entire macro name with the macro expansion. After that the C source code passes to the compiler.
Example:
#define TRUE 1
#define FALSE 0
Here C preprocessor searches for the macro name TRUE and FALSE in the C source code and substitute 1 and 0 each time it is encountered.
/*Program to find the area of circle using #define*/
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
double r,area;
printf("\nEnter radius of circle :");
scanf("%lf",&r);
area=PI*r*r;
printf("\nArea of a circle=%.2lf",area);
getch();
}
Output:
Enter radius of circle: 7
Area of a circle=153.86 cm2
Note: It is better to put all #define statement at the beginning of the program. We generally use capital letters for macro name. So we can easily identify where is define the macro expansion.
Definition of macro name is useful with the array. There are two way to take the size of the array, first is the array's size with a constant and second is to define the size of the array with #define directive. It is better to define a macro name that represents the size and use that macro name whenever the size of the array is needed.
The #include Directive
This preprocessor directive is used to include another file. It must be within angle bracket or double quotes. This is written as-
(a) #include "filename"
(b) #include<filename>
Where, # is a symbol used with directives.
After including the file the entire content of file can be used in the program. When the file is within angle bracket then the file is searched in the standard directory only. If the filename is in double quotes then first it is searched in the current directory, if it is not found then the file is searched in the standard directory.
Example-
#include<stdio.h>
main()
{
printf("I understand the use of include\n");
}
Output:
I understand the use of include
The #if AND #endif Directive
These are the conditional compilation directives. An expression which is followed to the #if is evaluated, if the result is true then the block of statement between #if and #endif is compiled otherwise it is skipped. This is written as-
#if(expression)
statements;
#endif
Example
#define FLAG 1
#include<stdio.h>
main()
{
#if FLAG==1
printf ("Result is true\n");
#endif
}
Output:
Result is true
The #else AND #elif
#else is used with the #if preprocessor directives. It is same as the else statement of the if....else control statement. First the #if is evaluated if the result is false then #else part is compiled.
Example
#define FLAG 0
#include<stdio.h>
main()
{
#if FLAG==1
printf("Result is true\n");
#else
printf("Result is false\n");
#endif
}
Output:
Result is false
Here the value of the flag is not equal to 1. Hence the part of the #else is compiled.
It is not necessary to take #endif before #else because #else show the end of the #if code. Nesting of #if and #else is also possible and this can be done with the #elif. Every #elif has one expression, first the expression is evaluated if the value is true then that part of code are compiled and all other #elif expression are skipped otherwise the next #elif expression is evaluated.
Syntax:
#if expression
statements
#elif expression
statements
....................
.....................
#endif
Example
#define FLAG 2
#include<stdio.h>
main()
{
#if FLAG= =0
printf("Value is zero\n");
#elif FLAG==1
printf("Value is one\n");
#elif FLAG==2
printf("Value is two\n");
#elif FLAG==3
printf(Value is three\n");
#endif
}
Output:
Value is two
The #ifdef AND #ifndef
These are also conditional compilation directives. #ifdef means "if defined" and #ifndef for "if not defined". If the macro name is already defined by #define statement then the statement of #ifdef is compiled.
Syntax:
#ifdef macro-name
Statements
#endif
Similarly,
#ifndef macro-name
Statements
#endif
The #undef Directive
This preprocessor directive is used for undefining the macro name which is previously defined by the #directive.
Syntax-
undef macro-name
Example
#define FLAG 1
#include<stdio.h>
main()
{
#ifdef FLAG
printf("FLAG is defined \n");
#endif
#undef FLAG
#ifndef FLAG
printf("Now FLAG is undefined\n");
#endif
}
Output:
FLAG is defined
Now FLAG is undefined
The #error Directive
This preprocessor directive is used for debugging purpose #error directive stops the compilation and display the message attached with this.
Syntax:
#error message