Macros And Its Types In C/C++

Macros And Its Types In C/C++

A macro in a program is a piece of code that is replaced by its defined value. It is defined using the #define directive in C and C++. When the compiler encounters the name of a macro, it replaces that name with the macro’s defined value. Unlike regular C statements, macro definitions do not need to be terminated by a semicolon (;). Macros are used for code replacement and are often employed for defining constants, inline functions, and conditional compilation directives in C and C++ programs.

Programs – To illustrate the use of macros in C/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;
}

Output

The value of LIMIT is 5

Program 2



// 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;
}

Output

Area of rectangle is: 50

Explanation

In this case, when the compiler encounters AREA(l, b) in the program, it replaces it with the macro’s definition, which is (l*b). So, if you use AREA(10, 5) in your code, it will be equal to 10*5. Macros provide a way to perform simple text replacement in your code, which can be helpful for defining constants or creating code templates for repetitive tasks.

Type Of Macros

Object-like macros are simple identifiers that are replaced with code fragments. They are called “object-like” because they resemble objects in the code where they are used. These macros are commonly used to replace symbolic names with constant values or variables, providing a convenient way to make code more readable and maintainable.

Illustration of Simple Macro

// 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;
}

Output

Lockdown will be extended upto 31-MAY-2020

2. Chain Macros: Macros inside macros are referred to as Chain Macros. Initially, the Parent Macro is being expanded and then the child macro is expanded.

The illustration of child macro is given below



// C program to illustrate macros
#include <stdio.h>
  
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
  
// Driver Code
int main()
{
    // Print the message
    printf("Skill Vertex  have %dK"
           " followers on Instagram",
           INSTAGRAM);
  
    return 0;
}

Output

Skill Vertex have 138K followers on Instagram

This is called “chaining of macros.” For example, “INSTAGRAM” expands to “FOLLOWERS,” and then “FOLLOWERS” further expands to “138K.”

3. Multi-line Macros

An object-like macro will contain multi-line. Hence, you need to require a backslash-newline for creating a multi-line macro.

The illustration of multiline macros are given below

// 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;
}

Output

Elements of Array are:
1  2  3

4. Function-like Macro:

Function-like macros in C are similar to function calls and replace code with a set of instructions. To use them, you need to include a pair of parentheses immediately after the macro name. If you add a space between the macro name and the parentheses in the definition, the macro won’t work.

Function-like macros are expanded only when their name is followed by a pair of parentheses. If you omit the parentheses, it can lead to a syntax error because the compiler may interpret it as a function pointer getting the address of the real function.

The illustration of function-like macros is given below:

// 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;
}

Output

Minimum value between 18 and 76 is 18

FAQ- Macros And Its Types In C/C++

Q1. What is macro and its types?

Ans. There are two types of macros:
Executive Macros: These macros generate code or data that becomes part of the assembled program. They typically produce executable instructions.
Declarative Macros: These macros produce information that aids the assembly process in generating code. They provide data or directives that guide the assembly process.
These two types of macros serve different purposes in the context of macro processing and code generation during assembly.

Q2. What is a macro type in C++?

Ans. In C++, macros are code snippets that can be replaced by specific values. There are different types: object-like (simple identifiers), function-like (similar to functions), multi-line (span multiple lines), and chain macros (macros within macros, allowing nesting). They simplify code in C++ programming.

Q3. Why use macros in C?

Ans.In C programming, macros are used to reuse values or code throughout a program. They are defined once and can be employed repeatedly. Additionally, C offers predefined macros.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment