Preprocessor Directives extend the power of the C programming language. C Preprocessors Directives is a special set of instructions in the program which is processed before handing over the program to the compiler. These instructions are always preceded by a pound sign (#) and NOT terminated by a semicolon. The preprocessor directives are executed before the actual compilation of code begins. Different preprocessor directives (commands) tell the preprocessor to perform different actions. We can categorize the Preprocessor Directives as follows:
- File inclusion directives
- Macro substitution directives
- Conditional compilation directives
Advantages:
- It makes program development easy
- It enhances the readability of the program
- It makes modification easy
- It increases the transportability of the program between different machine architectures.
File inclusion directives
The file inclusion directive is used to include files into the current file. When c preprocessors encounters an #include directive it replaces it with the entire content of the specified file. The file inclusion directive (#include) can be used in the following two ways:
- #include “file-name”
- #include <file-name>
We can see that #include can be used in two ways angular brackets (<>)and inverted commas (“”). The only difference between both expressions is the location (directories) where the compiler is going to look for the file. In the first case where the file name is specified between double-quotes, the compiler will look for the in the current directory that includes the file containing the directive. In case it is not there the compiler will look at the file in the default including directories where it is configured to look for the standard header files. When #include is written with <> it means the file is searched directly where the compiler is configured to look for the standard header files. Therefore, standard header files are usually included in angle brackets, while other specific header files are included using quotes.
Take a look at the below example program. Save the following code in a file called main.c:
1 2 3 4 5 6 7 8 |
#include<stdio.h> #include "head.h" int main() { printf("The variable from the head file: h = %d", h); return 0; } |
Then save the code below in a file called head. hand places it in the same directory as the main.c:
1 2 3 |
#ifndef __mynamespace__ int h = 5; #endif |
The variable is declared in the head.h file will be available in main.c, now we can use the variable in main().
Macro substitution directives
Macro substitution directives are used to define an identifier that is being replaced by a pre-defined string in the program. Macro substitution is a process in which the preprocessor replaces identifiers with one, or more program statements (like functions) and they are expanded inline.
There are two directives for Macro Definition:
- #define – Used to define a macro
- #undef – Used to undefine a macro
#define c preprocessors
To define preprocessor macros we can use #define.
Syntax:
1 |
#define identifier token_string |
Example:
1 |
#define PI 3.14 |
The token string in above line 3.14 is replaced in every occurrence of symbolic constant PI.
C Program to find the area of a circle.
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> #define PI 3.14 int main(){ int radius; float area; printf("Enter the radius: "); scanf("%d",&radius); area=PI*radius*radius; printf("Area=%.2f",area); return 0; } |
Output:
1 2 |
Enter the radius: 3 Area=28.27 |
Conditional compilation directives(#ifdef, #ifndef, #if, #endif, #else and #elif)
Conditional Compilation Directives allows you to include or exclude certain part of code only when certain condition are met. These macros are evaluated on compile time.
The following directives are included in this category:
- #if
- #elif
- #endif
- #ifdef
- #ifndef
Line control (#line) directive:
The #line directive gives us a way to control or set the value of the __LINE__ and __FILE__ macros. (Note: the filename is optional.) The __LINE__ and __FILE__ macros represent the current line being read and the current file being read.
Syntax:
1 |
#line number "filename" |
Error directive (#error)
Error directive allows to aborts the compilation process when it is found, ant reports a fatal error that can be specified as its parameter.
1 2 3 |
#if !defined(__cplusplus) #error C++ compiler required. #endif |
Pragma directive (#pragma)
The #pragma directive is a compiler-specific directive used for the suppression of specific error messages.
Predefined Macros
__LINE__
It represents the line number of the current source file being compiled.
__FILE__
It represents the name of the source file being compiled.
__DATE__
It returns the date, in the form “Mmm dd yyyy”, when the compilation process began.
__TIME__
A string literal in the form “hh:mm: ss” containing the time at which the compilation process began.
__STDC__
Forces the use of standard C/CPP codes only in the program.