Macros and its types in C
Last Updated :
11 Oct, 2024
In C, a macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).
Examples of Macros in C
Below are the programs to illustrate the use of macros in C:
Example 1
The below example demonstrates the use of macros to define LIMIT.
C
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define LIMIT 5
// Driver Code
int main()
{
// Print the value of macro defined
printf("The value of LIMIT"
" is %d",
LIMIT);
return 0;
}
OutputThe value of LIMIT is 5
Example 2
The below example demonstrates the use of macros to find the area of a rectangle.
C
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define AREA(l, b) (l * b)
// Driver Code
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
printf("Area of rectangle"
" is: %d",
area);
return 0;
}
OutputArea of rectangle is: 50
Explanation
From the above program, we can see that whenever the compiler finds AREA(l, b) in the program it replaces it with the macros definition i.e., (l*b). The values passed to the macro template AREA(l, b) will also be replaced by the statement (l*b). Therefore, AREA(10, 5) will be equal to 10*5.
If you want to understand how macros are used with data structures and optimization, the C Programming Course Online with Data Structures provides practical lessons on using macros effectively.
Types Of Macros in C
There are two types of macros in C language:
1. Object-Like Macros
An object-like macro is a simple identifier that will be replaced by a code fragment. It is called object-like because it looks like an object in code that uses it. It is popularly used to replace a symbolic name with a numerical/variable represented as a constant.
Example
The below example is the illustration of a simple macro i.e. object-like macros.
C
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define DATE 31
// Driver Code
int main()
{
// Print the message
printf("Lockdown will be extended"
" upto %d-MAY-2020",
DATE);
return 0;
}
OutputLockdown will be extended upto 31-MAY-2020
2. Chain Macros
Macros inside macros are termed chain macros. In chain macros first of all parent macro is expanded then the child macro is expanded.
Example
The below program is the illustration of a Chain Macro
C
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
// Driver Code
int main()
{
// Print the message
printf("Geeks for Geeks have %dK"
" followers on Instagram",
INSTAGRAM);
return 0;
}
OutputGeeks for Geeks have 138K followers on Instagram
Explanation:
INSTAGRAM is expanded first to produce FOLLOWERS. Then the expanded macro is expanded to produce the outcome as 138K. This is called the chaining of macros.
3. Multi-Line Macros
An object-like macro could have a multi-line. So to create a multi-line macro you have to use backslash-newline.
Example
Below is the illustration of multiline macros:
C
// C program to illustrate macros
#include <stdio.h>
// Multi-line Macro definition
#define ELE 1, \
2, \
3
// Driver Code
int main()
{
// Array arr[] with elements
// defined in macros
int arr[] = { ELE };
// Print elements
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OutputElements of Array are:
1 2 3
4. Function-Like Macros
These macros are the same as a function call. It replaces the entire code instead of a function name. Pair of parentheses immediately after the macro name is necessary. If we put a space between the macro name and the parentheses in the macro definition, then the macro will not work.
A function-like macro is only lengthened if and only if its name appears with a pair of parentheses after it. If we don’t do this, the function pointer will get the address of the real function and lead to a syntax error.
Example
The below example is the illustration of function-like macros
C
// C program to illustrate macros
#include <stdio.h>
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
// Driver Code
int main()
{
// Given two number a and b
int a = 18;
int b = 76;
printf("Minimum value between"
" %d and %d is %d\n",
a, b, min(a, b));
return 0;
}
OutputMinimum value between 18 and 76 is 18
Here is one example that will give more understanding of Macros:
Problem: We need to find the area of a circle by defining AREA(r) Macros.
C
#include <stdio.h>
#define PI 3.1416
#define AREA(r) (PI*(r)*(r))
int main() {
float r = 7; // radius of circle
printf("Area of Circle with radius %.0f: %.3f", r, AREA(r));
return 0;
}
// This code is contributed by Susobhan Akhuli
OutputArea of Circle with radius 7: 153.938
Conclusion
Macros are small pieces of code that can be executed with a single command. They can be used to automate tasks, simplify complex processes, and improve productivity. However, there are also some disadvantages to using macros. Here are some of the advantages and disadvantages of macros:
FAQs on C Macros
Q1. What are the Advantages of Macros?
Answer:
- Increased productivity: Macros can save time and reduce errors by automating repetitive tasks.
- Customization: Macros can be customized to fit specific needs, allowing for greater flexibility in how tasks are completed.
- Consistency: Macros can help ensure consistency in tasks by following a set of predetermined instructions.
- Efficiency: Macros can perform tasks more quickly than humans, improving overall efficiency.
- Ease of use: Macros are easy to create and use, requiring little to no programming knowledge
Q2. What are the disadvantages of Macros?
Answer:
- Security risks: Macros can be a security risk if they are not properly secured or if they are used to execute malicious code.
- Limited functionality: Macros may not be able to perform more complex tasks, limiting their usefulness.
- Compatibility issues: Macros may not be compatible with all software applications, limiting their usefulness.
- Maintenance: Macros may require maintenance and updates, which can be time-consuming and costly.
- Dependence: Over-reliance on macros can result in decreased problem-solving skills and critical thinking.
Q3. Can a macro take arguments?
Answer:
Yes, macros can take arguments. For example, MAX is a macro that takes two arguments (a and b) and returns the maximum of the two.
Q4. Can macros be recursive?
Answer:
No, macros cannot be recursive. If a macro becomes recursive then definition refers to itself, and it will lead to an infinite loop during preprocessing.