Preprocessor Directives in C Programming
Introduction
- A program which processes the source code before it passes through the compiler is known as preprocessor.
- The commands of the preprocessor are known as preprocessor directives.
- It is placed before the main().
- It begins with a # symbol.
- They are never terminated with a semicolon.
Preprocessor Directives
The preprocessor directives are divided into four different categories which are as follows:
1. Macro expansion- There are two types of macros - one which takes the argument and another which does not take any argument.
- Values are passed so that we can use the same macro for a wide range of values.
Syntax:
#define name replacement text
Where,
name – it is known as the micro template.
replacement text – it is known as the macro expansion.
- A macro name is generally written in capital letters.
- If suitable and relevant names are given macros increase the readability.
- If a macro is used in any program and we need to make some changes throughout the program we can just change the macro and the changes will be reflected everywhere in the program.
Example : Simple macro
#define LOWER 30
void main()
{
int i;
for (i=1;i<=LOWER; i++)
{
printf("\n%d", i);
}
}
Example : Macros with arguments
#define AREA(a) (3.14 * a * a)
void main()
{
float r = 3.5, x;
x = AREA (r);
printf ("\n Area of circle = %f", x);
}
Some of the predefined macros which are readily available are as follows:
Macro | Description |
---|
___LINE___ | It contains a current line number as a decimal constant. |
___FILE___ | It contains the current filename as a string literal. |
___DATE___ | It shows the current date as a character literal in the “MMM DD YYYY” format. |
___TIME___ | It shows the current time as a character literal in “HH:MM:SS” format. |
___STDC___ | It is defined as 1 when the compiler complies with the ANSI standard. |
___TIMESTAMP___ | It is a sing literal in the form of “DDD MM YYYY Date HH:MM:SS”. It is used to specify the date and time of the last modification of the current source file. |
2. File inclusion- The file inclusion uses the #include.
Syntax:
#include filename
- The content that is included in the filename will be replaced at the point where the directive is written.
- By using the file inclusive directive, we can include the header files in the programs.
- Macros, function declarations, declaration of the external variables can all be combined in the header file instead of repeating them in each of the program.
- The stdio.h header file contains the function declarations and all the information regarding the input and output.
There are two ways of the file inclusion statement:
i) #include “file-name”
ii) #include <file-name>
- If the first way is used, the file and then the filename in the current working directory and the specified list of directories would be searched.
- If the second way, is used the file and then the filename in the specified list of directories would be searched.
3. Conditional compilation- The conditional compilation is used when we want certain lines of code to be compiled or not.
- It uses directives like #if, #elif, #else, #endif
Syntax
#if TEST <= 5
statement 1;
statement 2;
#else
statement 3;
statement 4;
#endif
If there are a number of conditions to be checked we can use the #elif instead of #else and #if.
4. Miscellaneous directive
There are some directives which do not fall in any of the above mentioned categories.
There are two directives:
i) #undef : This directive is used in relation to the #define directive. It is used to undefine a defined macro.
ii) #pragma : It is a specialized and rarely used directive. They are used for turning on and off certain features.
Summary of preprocessor directives
Following table will show you various directives that we have studied in this chapter:
Directives | Description |
---|
#define | It substitutes a preprocessor macro. |
#include | It inserts a particular header file from another file. |
#undef | A preprocessor macro is undefined. |
#ifdef | It returns true if the macro is defined. |
#ifndef | It returns true if the macro is not defined. |
#if | It tests if the compile time condition is true. |
#else | It is an alternative for #if. |
#elif | It has #else and #if in one statement. |
#endif | The conditional preprocessor is ended. |
#error | It prints the error message on stderr. |
#pragma | It issues special commands to the compiler by using a standardized method. |