Explain the pre-processor directives in C language


The pre-processor is a program which process the source code before it passes through compiler. It operates under the control of different command lines or directives.

Pre-processor are placed in source program before the main line, it begins with symbol# in column one and do not require a semicolon at the end.

The commonly used pre-processor directives are −

  • #define,
  • #undef,
  • #include,
  • #ifdef,
  • #endif,
  • #if,
  • #else….

The pre-processor directives are divided into three categories −

  • Macro substitution directive.
  • File inclusion directive.
  • Compiler control directive.

Macro Substitution directive

The syntax and an example is as follows −

Syntax

Given below is the syntax for macro substitution directive −

#define identifier string

Example − simple macro: #define MAX 500

           Macro with arguments − #define sqrt(x) x*x

            Nested Macro − #define A 10

                     #define B A+1

File Inclusion Directive

The syntax and an example is as follows −

Syntax

Given below is the syntax for file inclusion directive −

#include “filename” or #include<filename>

For example,

#include<stdio.h>
#include "FORM.C"

Compiler Control Directives

These are used to control the compiler actions. C pre=processor offers a feature called as conditional compilation, which can be used to switch on or off based on particular line or group of lines in a program.

For example, #ifdef and #ifndef

Updated on: 15-Mar-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements