Unit 4 Preprocessor Directives: 1 What Is Pre-Processor?
Unit 4 Preprocessor Directives: 1 What Is Pre-Processor?
Unit 4 Preprocessor Directives: 1 What Is Pre-Processor?
1 What is pre-processor?
The job of C preprocessor is to process the source code before it is passed to the compiler.
The pre-processor gets the source code (test.c file) as input and creates expanded intermediate
source code (test.I file). This expanded source code is then passed to compiler for compilation.
The Pre-processor accepts source code as input and is responsible for
Removing comments.
Interpreting special pre-processor directives denoted by #.
Preprocessor
Directive Description
Directives
Macro #define Used to define a macro
expansion
#undef Used to undefine a macro
File inclusion
directives #include Inserts a particular header from another file
The definition should start with the keyword #define and should follow by identifier and a token
with at least one blank space between them.
The token may be any text and identifier must be a valid C name. The pre-processor replaces
every occurrence of the identifier in the source code by token.
There are different forms of macro substitution. The most common form is:
1) Simple macro substitution.
2) Argument macro substitution.
Simple token replacement is commonly used to define constants.
Example:
#define PI 3.1415926
Example:
#include<stdio.h>
#define TEN 10
void main ()
{
int a=10;
if (a == TEN)
{
print (“The value of a is 10”);
}
}
Output:
The value of a is 10
Macro function provide faster program running speed of the program fragment. The pre-
processor permits us to define more complex and more useful form of replacements it takes the
following form( Argument macro substitution).
#define identifier(f1,f2,f3....fn) token
C source code below shows the appropriate use of macro Function.
Example:
#include<stdio.h>
#include<conio.h>
#define SQUARE(x) (x)*(x)
int main()
{
clrscr();
printf (“%d”, SQUARE(10));
return 0;
}
Output:
100
4 What is #undef directive?
If you have created a macro definition, you can use #undef to remove it. #undef directive causes
a defined name to become undefined.
This means the pre-processor will no longer make anymore text substitutions associated with
that word.
A defined macro can be undefined using following the statement:
#undef identifier
For example #undef VALUE would cause the definition of VALUE to be removed from the
system.
Un-defining macro is useful when we want to restrict the definition only to a particular part of a
program. Also to change a definition, you must use #undef to undefine it and then use #define
to reduce it. C Source code below shows the use of #undef pre-processor.
Example:
#include<stdio.h>
#define PI 3.14
#define AREA(x) (PI)*(x)*(x)
int main ()
{
printf (“%f”, AREA(10));
#undef PI /*PI can no longer be used*/
return 0;
}
Output:
314.000000
Formatted Unformatted
scanf() getch()
printf() putch()
getche()
getchar()
putchar()
gets()
puts()
8 List out pre-defined macros in ctype.h.
The header file ‘ctype.h’ also contains a set of macros. Following table lists all the macros
available in ctype.h header file. These macros take an argument of integer type and return an
integer.