Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Input/Output statements
  5. Pre-processor directives
  6. Macro replacement directive

Macro replacement directive

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments