What is Preprocessor in C?

A Preprocessor in C is a program that processes or analyzes the source code file before it is given to the compiler. But actually what is Preprocessor in C? What is its role in C?

A Preprocessor in C plays an important role in the program execution process. In the C program development cycle, the source code (.C program) is given to the preprocessor before it is compiled.

Advantages of Preprocessor in C

  • It takes action according to special instructions called Preprocessor directives which begin with #.
  • Joins any lines that end with a backslash character into a single line.
  • Divides the program into a stream of tokens.
  • Remove comments and replaces them by a single space.
  • Processes preprocessor directives and expands macros.
  • Replaces escape sequences by their equivalent internal representation.
  • Concatenates adjacent constant character strings.
  • Replaces trigraph sequences (Non-ASCII character sets) by their equivalent.

The following flowchart shows the sequence of operations of the preprocessor:

what is preprocessor in c

Format of Preprocessor Directives

The preprocessor in C checks the source code for special instructions called preprocessor directives. Pre-processor directives are not ‘C’ language statements but they are special instructions for the pre-processor. The format of the directive is:

A preprocessor directive begins with # and does not end with semicolon(;)

Preprocessor directives in C are of three types:

  1. File inclusion directive.
  2. Macro substitution directive.
  3. Conditional compilation directive.

1. File Inclusion Directive

The file inclusion directive is #include. This directive instructs the compiler to include contents of the specified file, i.e, it inserts the entire contents of the file at that position.

Including a header file is equal to copying the contents of the header file.

Syntax:

#include<filename> OR #include “filename”

In the first format, the filename is searched in standard directories only. In the second, the file is first searched in the current directory. If it is not found there, the search continues in the standard directories.

Any external file-containing user-defined functions, macro definitions etc, can be included. An included file can include other files.

Example 1:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
#include<stdio.h>
#include<string.h></pre>
</div>

Example 2:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
/* group.h */
#include<stdio.h>
#include<math.h>
#include "myfile.c"
/*mainprogram.c*/
#include "group.h"
void main()
{
...
...
}</pre>
</div>

2. Macro Substitution Directive

Macro substitution is a process where an identifier in a program is replaced by a predefined string or a value. The macro substitution directive is #define.

Different types of macros in C substitution:

  1. Simple macro.
  2. Argumented macro.
  3. Nested macro.

Simple Macro Substitution:

It defined a symbol which represents a value. Wherever the macro name or symbol occurs in a program the pre-processor substitutes the code of the macro at that position.

It is used to define symbolic constants which occurs frequently in a program.

Syntax:

#define macro-id value

Examples:

  1. #define PI 3.142
  2. #define TRUE 1

Program:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
#include<stdio.h>
#define PI 3.142
void main()
{
float radius,area;
printf("Enter the radius");
scanf("%%f", &radius);
area=PI*radius*radius;
printf("Area=%%f",area);
}</pre>
</div>

Output:

what is preprocessor in c

Argumented Macros:

A macro can be defined with arguments. Such a macro is called a function macro because it looks like a function. It represents a small executable code. Each time the macro name is encountered, its arguments are replaced by the actual arguments in the program.

Example:

#define HALFOF(x) x/2 #define SQR(x) x*x #define MAX(x,y) ((x)>(y)?(x):(y))

Wherever the macro occurs in the program, its argument(example:x) is replaced by the variable or value used in the program.

For Example:

result=HALFOF(10);

It is advisable to enclose the arguments in ( ). If the arguments are not enclosed in ( ), it may yield wrong results.

Example: result=HALFOF(10+2);

This will be evaluated as result=10+2/2; and will give incorrect results. The correct way to define the macro would be:

#define HALFOF(x)

Now, result=(10+2)/2; which will give the correct result.

Nested Macros:

A macro can be used within another macro. this is called nesting of macros.

Example: #define CUBE(x) SQR(x)*(x)

In this example, macro SQR is called in a macro named CUBE.

A Macro can also be passed to itself as an argument.

#define MAX(a,b)

#define MAXTHREE(a,b,c)

3. Conditional Compilation Directive

this type of preprocessors in C causes conditionally suppress the compilation portion of the source code.

There are six types of Conditional Compilation Directive:

  1. #if directives.
  2. #elif directives.
  3. #ifdef directives.
  4. #ifndef directives.
  5. #else directives.
  6. #endif directives.

Example:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
#ifdef MACNAME
/*tokens added if MACNAME is defined */
#if TEST <=10
/*tokens added if MACNAME is defined and TEST <= 10 */
#else
/*tokens added if MACNAME is defined and TEST >  10 */
#endif
#else
/*tokens added if MACNAME is not defined */
#endif</pre>
</div>

If you want to learn preprocessor in c in Hindi then visit here.

Conclusion:

So in this lesson, we have learned about what is a preprocessor in C. So in this way we have completed all the lessons of C language. If you have any doubt then feel free to ask in the comment section below. Please to understand better do more and more practise of C programs.

Also Read: