Preprocessor
Directives
CHAPTER OUTLINE
11.1 Introduction
11.2
The #def ine Directive
11.3
Undefining a Macro
11.4
Token Pasting and Stringizing Operators
11.5
The #inciude Directive
11.6 Conditional Compilation
11.7
The #ifndef Directive
11.8
The #error Directive
11.9
The #lin e Directive
11.10 Inline Directive
11.11
The #pragma Saveregs
11.12
The ttpragma Directive
11.13
The Predefined Macros in AN SI and Turbo-C
11.14
Standard I/O Predefined Streams in s td io . H
11.15 The Predefined Macros in ctype. H
Exercises
11.1 INTRODUCTION
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
OR
#define identifier (argument 1... argument N) substitute text
Example
# 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 ()
{
float r,area;
printf (* Enter radius of circ le in cmB.m) ;
scanf ( ^ f * ,&r) ;
area*=PI*r*r;
prin tf ("Area of a Circle - %.2f cm2*,area) j
getcheO;
}
OUTPUT:
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 ()
{
int k;
els;
for (k=l;k<=N;k++)
display ("%dt",k);
ivaitO;
I
OUTPUT;
123456789 10
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 >
void main()
{
int a,b,c;
clrscr ();
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);
else
if(b larger a and b larger c)
printf ("% d are larger number ",b);
else
if(c larger a and c larger b)
printf ("% d are larger number ",c);
else
if (a equal b and b equal c)
printf (" n Numbers are same.");
I
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.194