19. C-PreProcessor

C-Storage Classes: Previous                                                            Next: C-Header Files
The pre processors are collection of statements called directives. These statements are executed before the actual compilation process starts. The pre processor works on source code i.e. ‘.c’ file and creates “Expanded Source code” because after this process all functions replaces their definition.

These statements are always starts with “#”(hash sign) as prefix in C Programming.
You must have these statements in previous example. Like #include. These are called preprocessor directives. These can be placed anywhere in the program but it is convenient to placed it at beginning of program.
Some rules for defining preprocessor directives: -
  • They must be placed in first column.
  • No two preprocessor directives can be placed in a single line.
  • They should not terminate with semicolon ( ; ).
The preprocessor directives are:
  1. Macro expansion
  2. File inclusion
  3. Conditional compilation
  1. Macro expansion: -C allows you to define an identifier having a constant value at the beginning of the program before the main( ) all macros are defined with # define
For Eg:     # include
#define LIMIT 10
void main()
{
int i;
for(i = 0; i
                printf(“%d\n”, i);
}

In above example instead of writing 10 in loop we have written LIMIT which is an identifier having constant value 10 and defined before void main( ). During preprocessing the every occurrence of LIMIT identifier replaced with its value ‘10’. The macro works as a constant value.
More than one macro can be defined to a single program. The macros are compulsory to define in upper case and values are compulsory to assign at the time of declaration.
#define can be used for defining:
  • An operator
  • Constant value
  • A condition
Eg: defining a operator
# include
#define OR ||
void main()
{
int num = 20;
if(num < 40) OR (num > 10)
                printf(“Example of Macro…!!”);
else
                printf(“Quite Accurate…!!”);
}

  1. File inclusion: -This directive starts with ‘#include’ and allow us to include one file into another file.
Syntax is:                                 #include ""  !---C Programming syntax
This statement causes content of defined to be inserted into program at the preprocessing time.
"" contains the prototype of the functions and for use those functions inclusion of file is compulsory.
Eg: you have used "stdio.h" which contains the definition of the functions printf( ), scanf( ), etc these function are called library functions.
#include can be written in two ways:

  1. #include": This command would look for specified file in current directory as well as in specified list of directories.
  2. #include: This command would look for file in specified list of directories only no searching in current directory.
  1. #Conditional (compilation directive):-It allows line of code to be passed on the basis of computed condition given in directive some preprocessor conditional commands are #if, #else, #endif etc.
#if and #else they work in same way as keyword of conditional branching works like #if directive is used to test the condition.
Syntax:
#if
                                Statements;
                #else
                                Statements;
                #endif
“ #endif ” directive to use to show the conditional directive block.

C-Storage Classes: Previous                                                            Next: C-Header Files