Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Macros and its types in C/C++

  • Difficulty Level : Basic
  • Last Updated : 19 Oct, 2021

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 semi-colon(;).
Below are the program to illustrate the use of macros in C/C++:
Program 1: 

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

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

C++




// C++ program to illustrate macros
#include <iostream>
using namespace std;
 
// Macro definition
#define LIMIT 5
 
// Driver Code
int main()
{
    // Print the value of macro defined
    cout << "The value of LIMIT"
         << " is " << LIMIT;
 
    return 0;
}
Output: 
The value of LIMIT is 5

 

Program 2: 

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

C++




// C++ program to illustrate macros
#include <iostream>
using namespace std;
 
// 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
    cout << "Area of rectangle"
         << " is: ",
        area;
 
    return 0;
}
Output: 



Area 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
 Types Of Macros 

  1. Object-like Macros: An object-like macro is a simple identifier which 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 to numerical/variable represented as constant.
    Below is the illustration of a simple macro:

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

C++




// C++ program to illustrate macros
#include <iostream>
using namespace std;
 
// Macro definition
#define DATE 31
 
// Driver Code
int main()
{
    // Print the message
    cout << "Lockdown will be extended"
         << " upto " << DATE
         << "-MAY-2020";
 
    return 0;
}
Output: 
Lockdown will be extended upto 31-MAY-2020

 

2. Chain Macros: Macros inside macros are termed as chain macros. In chain macros first of all parent macro is expand then child macro is expanded. 
Below 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;
}

C++




// C++ program to illustrate macros
#include <iostream>
using namespace std;
 
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
 
// Driver Code
int main()
{
    // Print the message
    cout << "Geeks for Geeks have "
         << INSTAGRAM << "K followers on Instagram!";
 
    return 0;
}
Output: 
Geeks for Geeks have 138K followers on Instagram!

 

  1. Explanation: 
    INSTAGRAM is expanded first to produce FOLLOWERS. Then the expanded macro is expanded to produce the outtcome as 138. This is called the chaining of macros.
     
  2. 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. 
    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;
}

C++




// C++ program to illustrate macros
#include <iostream>
using namespace std;
 
// 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++) {
        cout << arr[i] << ' ';
    }
 
    return 0;
}
Output: 
Elements of Array are:
1  2  3

 

3. Function-like Macro: 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 an 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.
Below 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;
}

C++




// C++ program to illustrate macros
#include <iostream>
using namespace std;
 
// 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;
 
    cout << "Minimum value between"
         << a << " and " << b
         << " is: " << min(a, b);
 
    return 0;
}
Output: 
Minimum value between 18 and 76 is 18

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :