In C programming, we can use macros whenever we want to repeatedly use a single value or a piece of code in our programs. By defining macros once in our program, we can use them frequently throughout the program.
A macro is a block or set of program code that is replaced by the value of the macro. It is a fragment of code that is given a name. A macro is defined by the preprocessor directive #define, which performs the action to replace the macro name with the macro value at the time of preprocessing.
Macros are preprocessed which means that all the macros would be processed before program compiles. Macro definitions need not be terminated by semi-colon (;). Macros can be used for a variety of purposes, such as creating constants, simplifying complex expressions, or generating repetitive code.
The program efficiency is increased by using Macros in c programs. Rather than mentioning a piece of code repeatedly in the programs, we can define the constant value once and use it frequently.
We define a macro using the #define
preprocessor directive. The general syntax is:
#define MACRO_NAME macro_value
- MACRO_NAME is the name we give to macro.
- Macro_value is the code or value that will replace every occurrence of MACRO_NAME in the code.
Example: #define PI 3.14
- #define – Preprocessor Directive
- PI- Macro Name
- 3.14- Macro Value
Now, wherever we use PI
in our code, it will be replaced with 3.14
.
A macro definition is also specified by the #define statement which is followed by an open parenthesis, a list of identifiers separated by commas, a closing parenthesis and then the substitution text.
Example:
#define SUM(a,b) a+b
We can use SUM
with different values:
int result = SUM(5,5); // result is 10
Types of Macros in C Language:
The macro in C language is classified based on the different types of values that it replaces.
There are two types of macros:
- Object like Macros
- Function like Macros
Object like Macros:
A macro is replaced by the value in object-like macros. It is widely used to represent numeric constants. An object like macros is simply the macros that get replaced by certain values or segments of code.
For example: #define PI 3.14
Programming Example:
#include <stdio.h>
#define PI 3.1415
int main()
{
float radius, area;
printf(“Enter the radius: “);
scanf(“%f”, &radius);
area=PI*radius*radius;
printf(“Area=%.2f”,area);
return 0;
}
In the above example, in the introduction, we saw the macro with the name PI and value 3.14, it is an example of an object like macros.
Function like Macros:
We can also define macros that work in a similar way like a function call. This is known as function like macros. Function like macros are very similar to the actual function in C programming. We can pass the arguments with the macro name and perform the actions in the code segment.
For example: #define MAX(a,b) (a>b)?a:b
Programming Example:
#include<stdio.h>
#include<conio.h>
#define sq(a) a*a
int main()
{
int x;
clrscr();
for(x=1;x<=10;x++)
printf(“\n%d”,sq(x));
getch();
return 0;
}
From the above program we can see that whenever the compiler finds sq(a) in the program it replaces it with the macros definition i.e., (a*a).
Advantage of using Macros:
A macro is a name given to a block of code which can be substituted where the code snippet is to be used for more than once.
- The speed of the execution of the program is the major advantage of using macros.
- It saves a lot of time that is spent by the compiler for invoking/calling the function.
- It is easy to modify the value of the constant variable, since we have to change the value at the macro definition only.