Preprocessor

Introduction:

Preprocessor directives are the actually the instructions to the compiler itself. They are operated directly by the compiler.

The most common preprocessor directives are:

  • include directive

  • defined directive

include directive: The include directive is used to include files.

Example:

#include<stdio.h>

Here stdio.h is a header file and the preprocessor replace the above line with the contents of the header file.

Define directory: It is used to assign the names to different constants or statements which are to be used repeated in the program.

These defined values or statement can be used by the main or other user defined functions as well.

Preprocessing directive #define has two forms.

  1. #define identifier token_string

  2. Macro with arguments

Note:

  1. Preprocessor statements begins with a hash symbol(#)

  2. Preprocessors are not terminated by a semicolon

define identifier token_string:

token_string: is optional but, are used almost every time in program

Example:

#define x 5

When x occurs, it is replaced by token string 5.

Example Program:

 Output:

Enter the radius : 2
Area=12.57

Explanation: The PI is a defined directive and is replaced by its value in every occurrence.

Macro with arguments: #define can be used to write macro definitions with parameters.

Syntax:

#define identifier(identifier 1,…i)

Token string: is optional but used in every case.

Example:

 Output:

Enter the radius : 2
Area=12.57

Explanation: Every time the program encounters area(radius), it wil replaced by (3.1415*(radius)*(radius)).