‘C’ preprocessors
Preprocessor is the program that processes the source code before it passes through the compiler.
- It is operated under control of preprocessor command lines or directives.
- Preprocessor directives are placed in the source program before the main() function.
- Action performed by the preprocessor is given in the followingdiagram:
Syntax rules for ‘C’ preprocessor:
- Preprocessor is the one line directive.
- Preprocessor directives are special instructions for the preprocessor.
- Preprocessors always begin with #, which must be firs character on the line.
- They do not require semicolon at the end of line.
List of commonly used directives is as below:
-
SR. No.
Directive Name
Function
1#define
Defines a macro substitution
2#undef
Undefines a macro
3#include
Specifies the file to be included
4#if
Test a compile time condition
5#else
Specifies alternatives when #if test fails.
6#ifdef
Test for a macro definition
7#endif
Specifies the end of #if
8#ifndef
Tests whether a macro is not defined
These directives can be categorized ito three categories.
- MACRO Substitution directives:
Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens.
- #define: It is used to define a macro.
Syntax:
#define <identifier> <string / expression>
- #undef: It is used to undefine a macro.
Syntax:
#undef <identifier>
This is useful when we want to restrict the definition only to a particular part of the program.
Examples:
Program 1) Using simple macro
#include<stdio.h>
#define pi 3.14
main()
{
float r,area;
clrscr();
printf("Enter r: ");
scanf("%f",&r);
area=pi*r*r;
printf("Area = %5.2f\n",area);
}
OUTPUT:
Enter r: 3.2
Area = 32.15
Program 2) Using macro with arguments.
#include<stdio.h>
#define square(x) x * x
main()
{
int n;
float res;
clrscr();
printf("Enter N: ");
scanf("%d",&n);
res = 60 / square(n);
printf("Res = %5.2f\n",res);
}
OUTPUT
Enter N: 2
Res = 60.00
Program 3) Using macro with arguments.
-
Source Code
OUTPUT
#include<stdio.h>#define square(x) (x * x)main(){int n;float res;printf("Enter N: ");scanf("%d",&n);res = 60 / square(n);printf("Res = %5.2f\n",res);}
Enter N: 2Res = 15.00
Program 4) Using macro with single arguments.
-
Source Code
OUTPUT
#include<stdio.h>#define square(x) x * xmain(){int n;float res;clrscr();printf("Enter N: ");scanf("%d",&n);res = square(n) / 4;printf("Res = %5.2f\n",res);}
Enter N: 6Res = 9.00
Program 5) Using macro with two arguments.
-
Source Code
OUTPUT
#include<stdio.h>#define max(a, b) ((a > b) ? a : b)main(){int a,b;printf("Enter a,b : ");scanf("%d %d",&a,&b);printf("Max = %d\n",max(a,b));}
Enter a,b : 7 5Max = 7
Enter a,b : 5 7Max = 7
Program 6) Using nested macro.
#include<stdio.h>
#define square(x) x * x
#define cube(x) x * square(x)
main()
{
int n;
clrscr();
printf("Enter N: ");
scanf("%d",&n);
printf("Cube = %d\n",cube(n));
}
OUTPUT
Enter N: 4
Cube = 64
Program 7) Using #undef #include<stdio.h>
#define max(a, b) ((a > b) ? a : b)
void main()
{
int a,b;
printf("Enter a,b : ");
scanf("%d %d",&a,&b);
if (max(a,b) != 0)
printf("Max = %d\n",max(a,b));
else
{
#ifdef max
#undef max
#endif
}
}
- File inclusion directives:
An external file containing functions or macro definitions can be included as a part of a program so that we need not rewrite those functions or macro definitions. This achieved by the preprocessor directive #include
Syntax :
#include<filename>
These format can be used when user want to use the standard functions and macros only. Means the file is searched only in the standard directories.
Or #include “file name”
Second format can be used when the functions and macros are defined in user defined files. At this point, the preprocessor inserts the entire contents of filename into the source code of the program. Here the file is first searched in the current directory and then I the standard directories.
Nesting of included files is allowed. That is, an included file can include othe files. However a file can not include itself.
If an included file is not found, an error is reported and compilation is terminated.