Macros in C are preprocessor directives that replace code before the compilation process, helping simplify and optimize programs. They are commonly used to define constants and reusable code snippets.
- Macros improve code readability, reusability, and maintainability.
- They improve code readability and can enhance execution efficiency by avoiding function call overhead.
#include <stdio.h>
// Macro definition
#define LIMIT 5
int main(){
// Print the value of macro defined
printf("LIMIT: %d", LIMIT);
return 0;
}
Output
LIMIT: 5
Explanation
- The #define directive creates a macro named LIMIT with the value 5, which is replaced by the preprocessor before compilation.
- When printf() uses LIMIT, it is replaced with 5, so the program prints the value 5.
Syntax
#define MACRO_NAME value
where MACRO_NAME is the name of the macro and value is the code or value that replaces the macro name. It is to be noted that macros don't necessarily need to be in uppercase. Instead, it is a convention that makes it easy to recognize.
Types Of Macros in C
There are two types of macros in C language:
1. Object-Like Macros
Object-like macros are the simplest type of macros. They replace the macro name with a defined value or expression. These are used for constants or simple values.
#include <stdio.h>
// Macro definition
#define DATE 31
int main(){
// Print the message
printf("Lockdown will be extended"
" upto %d-MAY-2020",
DATE);
return 0;
}
Output
Lockdown will be extended upto 31-MAY-2020
Explanation
- The object-like macro DATE is defined with the value 31 using the #define directive.
- Before compilation, the preprocessor replaces DATE with 31, so printf() prints the value as part of the output.
2. Chain Macros
These macros involve chaining multiple macros together. This can be done by combining different macros in a single macro definition, allowing for more complex operations. In chain macros first of all parent macro is expanded then the child macro is expanded.
#include <stdio.h>
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
int main(){
printf("Geeks for Geeks have %dK"
" followers on Instagram",
INSTAGRAM);
return 0;
}
Output
Geeks for Geeks have 138K followers on Instagram
Explanation
- The macro INSTAGRAM expands to FOLLOWERS, which is then expanded again to 138K by the preprocessor.
- This process of one macro expanding into another macro is known as macro chaining.
3. Multi-Line Macros
These macros span multiple lines for readability and organization. They are often used when you need a more complex expression or code block. To create a multi-line macro you have to use backslash \.
#include <stdio.h>
// Multi-line Macro definition
#define ELE 1, \
2, \
3
int main(){
// Array arr[] with elements
// defined in macros
int arr[] = { ELE };
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
1 2 3
Explanation
- The multi-line macro ELE is defined with the values 1, 2, 3 and is used to initialize the array arr[].
- Before compilation, the preprocessor expands ELE with the specified values, resulting in the array being initialized as {1, 2, 3}.
4. Function-Like Macros
Function-like macros accept parameters and work similarly to functions, allowing reusable logic that is expanded by the preprocessor during compilation. They are expanded only when the macro name is followed by parentheses containing arguments.
#include <stdio.h>
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
int main(){
// Given two number a and b
int a = 18, b = 76;
printf("Minimum: %d", min(a, b));
return 0;
}
Output
Minimum: 18
Explanation
- The function-like macro min(a, b) uses the ternary operator (?:) to compare two values and return the smaller one.
- When the values 18 and 76 are passed to the macro, it compares them and returns 18.