Table of Contents
show
Macros are defined with the help of define directive. The identifier name immediately following the define directive is called macro name
Types of macro
- Macro without arguments (object like macros)
- Macro with arguments (function like macros)
Object like macros
<strong>#define macro-name replacement-list</strong>
Example
#include<stdio.h>
#define PI 3.14
int main()
{
int rad = 5;
printf("Area = %f",PI*rad*rad);
return 0;
}
Output
Area = 78.500000
Function like macros
A macro with arguments is called a function like macro
Syntax
#define macro-name(parameter-list) replacement-list
Example
#include<stdio.h>
#define SQR(x) (x*x)
int main()
{
int side = 5;
printf("Area of square is %d",SQR(side));
return 0;
}
Output
Area of square is 25
Views: 0