What are the Pre-processor Commands in C language?


The preprocessor is a program that sends the source code before it passes through the compiler. It operates under preprocessor directives which begin with the symbol #.

Types

The three types of preprocessor commands are as follows −

  • Macro substitution directives.

  • File inclusion directives.

  • Compiler control directives.

Macro substitution directives

It replaces every occurrence of an identifier by a predefined string.

The syntax for defining a macro substitution directive is as follows −

# define identifier string

For example,

#define    PI    3.1415
#define    f(x)  x *x
#undef     PI

Example

Following is the C program for the macro substitution directive −

#define wait getch( )
main ( ){
   clrscr ( );
   printf ("Hello");
   wait ;
}

Output

When the above program is executed, it produces the following result −

Hello

File inclusion directives

An external file containing functions (or) macro definitions can be included by using #include directive.

The syntax for file inclusion directive is as follows −

# include <filename> (or) #include "filename"

Example

Following is the C program for the file inclusion directive −

 Live Demo

#include <stdio.h>
main ( ){
   printf ("hello");
}

Output

When the above program is executed, it produces the following result −

Hello

The definition of the function printf ( ) is present in <stdio.h>header file.

Compiler control directives

C preprocessor offers a feature known as a conditional compilation, which can be used to switch ON (or) OFF a particular line (or) group of lines in a program.

Example

Following is the C program for the compiler control directive −

 Live Demo

#if, #else, #endif etc.
#define LINE 1
#include<stdio.h>
main ( ){
   #ifdef LINE
   printf ("this is line number one");
   #else
   printf("This is line number two");
   #endif
}

Output

When the above program is executed, it produces the following result −

This line number one

Updated on: 08-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements