Pre-processor
• The C pre-processor is a process to add predefined functions and macros to our main code
automatically by the C compiler to transform our program before actual compilation. It is
also called as macro processor because it allows you to define macros.
• Types of Pre-processor Directive:
Statements which are used in pre-processor program are called pre-processor Directives.
1. File Inclusive Directives: These are files of declarations that can be substituted into your
program.
2. Macro substitution: You can define macros, which are abbreviations for C code, and
then the C pre-processor will replace the macros with their definitions throughout the program.
3. conditional compilation Directives: Using special pre-processing directives, you can
include or exclude parts of the program according to various conditions.
4. Miscellaneous Directives:
1. File Inclusive Directives:
Header Files: (#include)
• A header file is a file containing C declarations and macro definitions. we request the use
of a header file in our program with the C preprocessing directive `#include'.
• The header files names end with `.h'.
• Both user and system header files are included using the preprocessing directive `#include‘
• There are two ways to add header file in program-
#include <file>
#include "file"
Example: Creating an user header file
File name: arithmetic.h
#include<stdio.h>
int add(int a,int b)
{
return (a+b);
}
int sub(int a,int b)
{
return (a-b);
}
Example: use of an user header file
File name: program.c
#include<stdio.h>
#include “arithmetic.h”
int main()
{
int res;
res=add(10,5);
printf("sum=%d",res);
printf("\nsub=%d",sub(10,5));
return 0;
}
Output:sum=15
Sub= 5
2. Macro substitution:
What is macro:
• A macro is a segment of code which is replaced by the value of macro. Macro is defined by
#define directive.
Macro expansion: (#define)
Simple Macros
Syntax:
#define Template_name value
Note: Template_name replace with its value in program.
Example: #define upper 20
There are two types of macros:
1. Object-like Macros(simple Macros):
• The object-like macro is an identifier that is replaced by value. It is widely used to
represent numeric constants. For example:
Ex: #define PI 3.14
2. Function-like Macros(Macro with Arguments):
• The function-like macro looks like function call.
Syntax:
#define Template_name(Arguments,...) (Expression)
Example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Example:
#include<stdio.h>
# define rectanglearea(a,b) (a*b)
int main()
{
int a,b,area;
printf(“enter a & b value\n”);
scanf(“%d%d”,&a,&b);
area=rectanglearea(a,b);
printf(“area of rectangle is %d\n”,area);
}
3.Miscellaneous Directives: (#undef)
The #undef directive tells the pre-processor to remove the definitions for the specified
macro. A macro can be redefined after it has been removed by the #undef directive.
Syntax:
#undef Template_name(Arguments,...) (Expression)
Example:
# define rectanglearea(a,b) (a*b) //macro definition
# undefrectanglearea(a,b) (a*b) //macro undefined
# define rectanglearea(a,b) (a*b) //macro redefined
Example 1:
#include<stdio.h>
# define area(a,b) (a*b)
# undef area(a,b) (a*b)
int main()
{
int a=2,b=11;
printf("area of rectangle is %d", area(a,b));
return 0;
}
Output:
Error // area is not defined macro
Example 2:
#include<stdio.h>
# define area(a,b) (a*b)
# undef area(a,b) (a*b)
# define area(a,b) (a*b)
int main()
{
int a=2,b=11;
printf("area of rectangle is %d", area(a,b));
return 0;
}
Output:
area of rectangle is 22
Predefined macros:
1. __DATE__
2. __TIME__
3. __LINE__
4. __FILE__
4.Conditional compilation :(#ifdef, #ifndef, #endif)
Using conditional compilation, if user want, compiler to skip over part of code by using
preprocessing commands #ifdef and #endif
Syntax:
#ifdef macroname
Statement 1;
Statement 1;
#endif
Example 1:
#include<stdio.h>
# define rectanglearea(a,b) (a*b)
# define upper 2
int main()
int a,b,area;
#ifdef upper
printf("enter a & b value");
scanf("%d%d",&a,&b);
area=rectanglearea(a,b);
printf("area of rectangle is %d",area);
#endif
Printf(“\nend of the program”):
return 0;
Output:
enter a & b value
57
area of rectangle is 35
end of the program
Example 2:
#include<stdio.h>
# define rectanglearea(a,b) (a*b)
# define upper 2
int main()
int a,b,area;
#ifdef u
printf("enter a & b value");
scanf("%d%d",&a,&b);
area=rectanglearea(a,b);
printf("area of rectangle is %d",area);
#endif
Printf(“\nend of the program”):
return 0;
Output:
end of the program
Sr.No. Directive & Description
1 #define
Substitutes a preprocessor macro.
2 #include
Inserts a particular header from another file.
3 #undef
Undefines a preprocessor macro.
4 #ifdef
Returns true if this macro is defined.
5 #ifndef
Returns true if this macro is not defined.
6 #if
Tests if a compile time condition is true.
7 #else
The alternative for #if.
8 #elif
#else and #if in one statement.
9 #endif
Ends preprocessor conditional.