Preprocessor Directives
Preprocessor Directives
• 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.
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.
4. Miscellaneous Directives:
• 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'.
• Both user and system header files are included using the preprocessing directive `#include‘
#include <file>
#include "file"
Example: Creating an user header file
File name: arithmetic.h
#include<stdio.h>
#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
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);
}
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__
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
scanf("%d%d",&a,&b);
area=rectanglearea(a,b);
#endif
return 0;
Output:
57
area of rectangle is 35
Example 2:
#include<stdio.h>
# define upper 2
int main()
int a,b,area;
#ifdef u
area=rectanglearea(a,b);
#endif
return 0;
Output:
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.