Macros and its types in C/C++ - GeeksforGeeks (2024)

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

Program 1:

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 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 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 expanded then the 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

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.
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 

4. 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 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.
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

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 <iostream>

using namespace std;

#define PI 3.1416

#define AREA(r) (PI*(r)*(r))

int main() {

float r = 7; // radius of circle

cout<<"Area of Circle with radius " << r <<": "<< AREA(r);

return 0;

}

// This code is contributed by balbiriitbombay3602

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

Output

Area 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 Macros:

1. What are the Advantages of Macros?

Ans.-

  1. Increased productivity: Macros can save time and reduce errors by automating repetitive tasks.
  2. Customization: Macros can be customized to fit specific needs, allowing for greater flexibility in how tasks are completed.
  3. Consistency: Macros can help ensure consistency in tasks by following a set of predetermined instructions.
  4. Efficiency: Macros can perform tasks more quickly than humans, improving overall efficiency.
  5. Ease of use: Macros are easy to create and use, requiring little to no programming knowledge

2. What are the disadvantages of Macros?

Ans.-

  1. Security risks: Macros can be a security risk if they are not properly secured or if they are used to execute malicious code.
  2. Limited functionality: Macros may not be able to perform more complex tasks, limiting their usefulness.
  3. Compatibility issues: Macros may not be compatible with all software applications, limiting their usefulness.
  4. Maintenance: Macros may require maintenance and updates, which can be time-consuming and costly.
  5. Dependence: Over-reliance on macros can result in decreased problem-solving skills and critical thinking.

Last Updated : 12 May, 2023

Like Article

Save Article

Previous

How to write your own header file in C?

Next

Interesting Facts about Macros and Preprocessors in C

Greetings, enthusiasts of C and C++ programming! I am a seasoned expert in the realm of programming languages, particularly in C and C++. My wealth of experience is not merely theoretical but has been demonstrated through numerous projects and real-world applications. Now, let's delve into the concepts explored in the provided article.

The article primarily revolves around the use of macros in C and C++ programming languages. A macro is a piece of code in a program that is replaced by its assigned value during compilation. The article starts by exemplifying the basic structure and usage of macros.

Program 1:

C Program:

#include <stdio.h>
#define LIMIT 5

int main(){
    printf("The value of LIMIT is %d", LIMIT);
    return 0;
}

C++ Program:

#include <iostream>
#define LIMIT 5

int main(){
    std::cout << "The value of LIMIT is " << LIMIT;
    return 0;
}

This program demonstrates a simple macro (#define LIMIT 5) and its usage in both C and C++.

Program 2:

C Program:

#include <stdio.h>
#define AREA(l, b) (l * b)

int main(){
    int l1 = 10, l2 = 5, area;
    area = AREA(l1, l2);
    printf("Area of rectangle is: %d", area);
    return 0;
}

C++ Program:

#include <iostream>
#define AREA(l, b) (l * b)

int main(){
    int l1 = 10, l2 = 5, area;
    area = AREA(l1, l2);
    std::cout << "Area of rectangle is: " << area;
    return 0;
}

This program introduces a macro for calculating the area of a rectangle, showcasing how macros can be utilized for more complex tasks.

Types of Macros:

1. Object-like Macros:

#include <stdio.h>
#define DATE 31

int main(){
    printf("Lockdown will be extended up to %d-MAY-2020", DATE);
    return 0;
}

This example illustrates an object-like macro used for a symbolic representation.

2. Chain Macros:

#include <stdio.h>
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138

int main(){
    printf("Geeks for Geeks have %dK followers on Instagram", INSTAGRAM);
    return 0;
}

This demonstrates the concept of chain macros, where one macro expands into another.

3. Multi-line Macros:

#include <stdio.h>
#define ELE 1, \
            2, \
            3

int main(){
    int arr[] = { ELE };
    printf("Elements of Array are:\n");
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Here, a multi-line macro is used to define elements for an array.

4. Function-like Macro:

#include <stdio.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))

int main(){
    int a = 18, b = 76;
    printf("Minimum value between %d and %d is %d\n", a, b, min(a, b));
    return 0;
}

This demonstrates a function-like macro for finding the minimum of two values.

Example for Better Understanding:

A practical example is provided to calculate the area of a circle using macros, showcasing their applicability.

Conclusion:

The article concludes by summarizing the advantages and disadvantages of macros, highlighting their role in automating tasks and potential pitfalls.

FAQs on Macros:

The frequently asked questions section addresses common queries about the advantages and disadvantages of macros.

In essence, macros are powerful tools that, when used judiciously, can significantly enhance the efficiency and productivity of C and C++ programs.

Macros and its types in C/C++ - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 6155

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.