The #def ine Directive
Undefining a Macro
Token Pasting and Stringizing Operators
The #inciude Directive
11.6 Conditional Compilation
The #ifndef Directive
The #error Directive
The #lin e Directive
11.10 Inline Directive
The #pragma Saveregs
The ttpragma Directive
The Predefined Macros in AN SI and Turbo-C
Standard I/O Predefined Streams in s td io . H
11.15 The Predefined Macros in ctype. H
You are now is aware of the execution of C programs. For refreshing the knowledge of the reader, any
program execution needs certain steps. They are (a) the C program is written in the editor, (b)
compilation, (c) linking and (d) the executable code is generated. In between these stages there also
involves one more stage i.e. preprocessor. The preprocessor is a program that processes the source
program before it is passed on to the compiler.
The program typed in the editor is the source code to the preprocessor. The preprocessor then
passes the source code to the compiler. It is also nonessential to write the program with preprocessor
facility. But it is a good practice to use it preferably at the beginning.
One of the most important features of C language is to offer preprocessor directives. The
preprocessor directives are always initialized at the beginning of the program. It begins with a symbol
# (hash). It can be placed anywhere but quite often it is declared at the beginning before themain ()
function or any particular function.
11.2 THE #de fin e DIRECTIVE
The syntax of #def ine directive is as follows.
# define id e n tifie r <substitute text>
356 Programming and Data Structures
#define identifier (argument 1... argument N) substitute text
# deifne PI 3.14
This statement defines macro templates. During preprocessing the preprocessor replaces every
occurrence of PI (identifier) with 3.14 (substitute value). Here, PI is a macro template and 3.14 is its
macro expansion. The macro templates are generally declared with capital letters for quick
identification. One can also define macros with small letters. The macro templates and its expansions
must be separated with at least one blank space. It is not necessary to provide space between # and
define. It is optional to the programmer.
The macro definition can be terminated with semi-colon and it is optional. The programmer can
try the macro definition with and without semi-colons. The words followed by #- are not keywords.
The programmer can use these words for variable names.
A few examples are illustrated below for understanding macros.
11.1 Use the identifier for 3.1413 as PI and write a program to find the area of circle using it
# include <stdio.h>
# include <conio.h>
# define PI 3.14
void main ()
printf (*\n Enter radius of circ le in cmB.m) ;
scanf ( ^ f * ,&r) ;
prin tf ("Area of a Circle - %.2f cm2*,area) j
Enter radius of circle in cms.: 7
Area of a Circle =153.86 cm2
Explanation In the above program PI replaces 3.14 during the program execution- In the program
instead of writing the value of PI as 3 .14 we define directly the value PI as 3.14. The term PI is
replaced by 3 .14 and used for calculating the area of a circle.
11.2 Write a program to define and create identifier for 'c' statements and variables.
1) Read N as 10
2) Replace clrscrO with els
3) Replace getcheO with waitO
4) Replace printf with display
Preprocessor Directives 357
# Include <stdio.h>
# include <conio.h>
# define N 10
# define els clrscr ()
# define wait () getche ()
# define display printf
void main ()
Explanation In the above program preprocessor directives are defined as follows.
a) 10 replaces value of N. b) getche () is replaced bywai t (). Q p r in t f is replaced by display.
The preprocessor directives are executed first and then the program.
11.3 Write a program to define macros for logical operators.
# include <stdio.h># include <conio.h>
# define and &&
# define equal *= *
# define larger >
printf ("Enter Three Numbers.:");
scanf ("% d % d %d",&a,&cb,&cc);
if (a larger b and a larger c)
printf ("% d are larger number",a);
if(b larger a and b larger c)
printf ("% d are larger number ",b);
if(c larger a and c larger b)
printf ("% d are larger number ",c);
if (a equal b and b equal c)
printf ("\ n Numbers are same.");