What Pre-processor Directive do ? | Tasks Performed by Preprocessor Directive

Tasks Performed by Preprocessor Directive | What Pre-processor Directive do ?

What is Preprocessor :

Program that Processes or Analyzes the source code file before Given to the Compiler is called as Pre-Processor


Refer this Article to know better : [ What is preprocessor? ]

Tasks Performed by Pre-processor Directive in C

  • Replaces Trigraph Sequences
  • Joins any Line with the Backslash Character into Single Line
  • Divide Program into Set of Tokens
  • Expand Macros
  • Remove Comments and Replace it by Single Space
  • Represent the Escape Sequence by its Internal Representation
  • Concatenate adjacent Constant character String

1 .Replaces Trigraph Sequence

  1. Some characters from the C or C++ character set are not available on all keyboards.
  2. These characters can be entered into a C or C++ source program using a sequence of three characters called a trigraph.
----------------------
Trigraph  Replacement
----------------------
 ??=       #
 ??(       [
 ??<       {
 ??/       \
 ??)       ]
 ??>       }
 ??’       ˆ          
 ??!       |          
 ??-       ~
  • Preprocessor will Replaces these Trigraph sequences with equivalent Character. Suppose Programmer have written ??> inside program then preprocessor will replace this trigraph with “}“.

2. Joins any Line with the Backslash Character into Single Line

  1. We can break printf statement in two or more lines to make program more readable. [ How to break Printf Statement into multiple Lines ]
  2. Preprocessor Merges two lines on single line.
printf("Hello \
world");

Preprocessor does this -

printf("Hello world");

3. Expand Macro

#define MAX 20
main()
{
int i = MAX;
printf("%d",i);
}

After Preprocessor Program will be expanded as -

main()
{
int i = 20;
printf("%d",i);
}

4. Replacing Comments with space

Suppose programmer have written this code

main()
{
int i = 20;
/* this is first
        variable */
//Print the variable
//printf is used to print variable
printf("%d",i);
}

then -

  • Preprocessor will replace Comment with whitespace.
  • Comments are ignored by compiler.
  • Preprocessor will replace this Command with White Space as it is ignored by compiler.
  • After Replacing Comment Code will be like this -
main()
{
int i = 20;
printf("%d",i);
}

Note : Preprocessor Directive

  • They Begin with a # which must be first non-space character on the line
  • They do not ends with semicolon