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