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
Object-like macros
function-like macros
Object-like macros do no havet parameters, whereas function like have parameters.
#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;
}
0 Comment(s)