MACROS
A macro is a piece of statements(code) which we provide a name or give that piece of statements a name. so whenever we used that name which we provide to that piece of code in the program, that code get replaced by the contents of the macro.
Advantages of using macros.
- Macro effect the speed of execution of a program. It increase the speed of a execution of a program
-It saves a lot of time.
- It make program shorter in length.
TYPES OF MACROS
**SYNTAX OF MACROS**
#define identifier(identifier 1,identifier 2,identifier 3.....identifier n) token-string
#define is used for macro defination.
in above syntax of macros contain the token list part, this part is a optional part but it used mainly in every case.
example:- #define area(a) (3.14*(a)*(a))
Here, the argument passed is a. Every time the program encounters area(argument), it will be replace by perimeter(3.14*(argument)*(argument)).
C Program to find area of a circle by macros.
#include <stdio.h>
#define Pie 3.1415 // pie macros
#define area(a) (Pie*(a)*(a)) // area macros
int main()
{
int r;
float area;
printf("Enter the radius: ");
scanf("%d",&r);
area=area(r);
printf(" Area is=%f",area);
return 0;
}
OUTPUT
Enter the radius: 4
Area is=50.264
0 Comment(s)