Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Preprocessor Directives

Preprocessor Directives
 The preprocessor, is a program that processes the source program before
compilation.
 Preprocessor directives are placed in the source program before the main line.
 If there are any, appropriate actions (as per the directives) are taken and then
the source program is handed over to the compiler.
General Form
 Preprocessor directives follow special syntax rules that are different
from the normal C syntax.
 They all begin with the symbol # in column one and do not require
a semicolon at the end.
 #define identifier substitute
or
 #define identifier(argument-1 . . . argument-N) substitute
Commonly Used Preprocessor Directives
Categories of Directives
 It operates under the following directives
 File Inclusion
 Macro substitution
 Conditional inclusion
File Inclusion
 It is used to include some file that contains functions or
some definitions.
 Syntax:
#include<filename> (or)
#include“filename”
 Eg: #include<stdio.h>
#include “ex.c”
Example
#include<stdio.h>
#include "addition.txt"
Int main()
{
int a,b;
printf("\nEnter the numbers:");
scanf("%d%d",&a,&b);
printf("The Value is %d",add(a,b));
return 0;
}
addition.txt

int add(int a,int b)


{
return(a+b);
}
Output

Enter the numbers:7


4
The Value is 11
Example
#include<stdio.h>
#include "fact.c"
int main()
{
int a;
printf("\nEnter the number:");
scanf("%d",&a);
printf("The factorial of %d! is %d",a,rec(a));
return 0;
}
fact.c

int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
Output

Enter the number:5


The factorial of 5! is 120
Macro Substitution

 It is used to define and use integer, string, or identifier in


the source program
 The three forms of macros are
 Simple Macro
 Argumented Macro
 Nested Macro
Simple Macro

 It is used to define some constants


 Syntax
# define identifier string/integer
 Eg:
#define pi 3.14
#define CITY “chennai”
Example
#include<stdio.h>
#define pi 3.14
#define CITY "chennai"
int main()
{
printf("The Value is %f",2*pi);
printf("\nThe Value CITY is %s",CITY);
return 0;
}

Output:
The Value is 6.280000
The Value CITY is chennai
Argumented Macro
 It is used to define some complex forms in the source program.
 Syntax:
#define identifier (v1,v2,….) string/integer

 Eg:
#define cube(n) (n*n*n)
Example
#include<stdio.h>
#define cube(n) (n*n*n)
int main()
{
printf("The Value of 3 cube is %d",cube(3));
return 0;
}

Output:
The Value of 3 cube is 27
Nested Macro

 Here one macro is used by another macro.

 Eg:
#define a 3
#define sq a*a
Example
#include<stdio.h>
#define a 3
#define sq a*a
int main()
{
printf("The Value is %d",sq);
return 0;
}

Output:
The Value is 9
Conditional Inclusion

 It is used to include some conditional statements.


 Preprocessor offers a feature known as conditional
compilation, which can be used to ‘switch’ on or off a
particular line or group of lines in a program.
Example
 This situation refers to the conditional definition of a macro.
 We want to ensure that the macro TEST is always defined, irrespective of
whether it has been defined in the header file or not.
 This can be achieved as follows:
#include "DEFINE.H"
#ifndef TEST
#define TEST 1
#endif
Example
#include<stdio.h>
#define a 3
#ifdef a
#define c a+5
#endif
int main()
{
printf("\nThe value C is %d",c);
return 0;
}

Output:
The value C is 8
What will be the output of the following C code?
#include <stdio.h>
#if A == 1
#define B 0
#else
#define B 1
#endif
int main()
{
printf("%d", B);
return 0;
}
Output:
1

You might also like