Abstract
- Preprocessor replaces the Macro with the value before passing to the Compiler
#include <stdio.h>
#define PI 3.1415 // Macro Expansion!
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
// Notice, the use of PI
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
Memory efficiency
This avoids the use of variables which saves memory.
Important
Not to put
;
at the end, or it will be considered as part of the macro value!
Function-like Macro
Not same as Function, doesn’t have Type Safety
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main() {
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);
return 0;
}