Updated March 28, 2023
Introduction to Macros in C++
Macros are nothing but a piece of code in C++ programming language represented by some given names. Therefore, whenever you are running your source code and the same name is found by the code compiler then the compiler will replace the name by the original piece of code. Macros are slightly popular in programming because it can do numerous thing which no compiler can ever do. For every piece of code, we can define a macro by giving it some name to avoid writing lengthy codes. As with the help of macros code reusability increase to the next level.
Syntax for representing macros in C++:
#define AREA ( l , b )( l * b )
# is the pre-processor that directs the compiler to preprocess before compiling the source code. The word AREA in the macro definition is called a Macro definition and ( l , b ) ( l * b ). Therefore when you run a code then every time a compiler will come across AREA(l,b) it will replace the value with (l * b). For example, if in the code it is written like #define AREA (15,16) then the result will be 240.
Now we will discuss how does a macro works in programming through C++ code. Because it is important to know how to correctly apply the syntax in source code so that it can be reused again and again whenever it is needed.
Examples to Implement Macros in C++
Below are the examples of Macros in C++:
Example #1
Here is the C++ code to demonstrate the working of Macros in C++ programming:
Code:
#include <iostream>
using namespace std ;
#define DEBUGGING // defining the MACRO debugging
#define MIN(a,b) ( ( ( a ) < ( b ) ) ? a : b )
int main () {
int x, y ; // declaring two integers x and y
x = 50 ;
y = 15 ;
#ifdef DEBUGGING
cerr << " we are inside the main function " << endl ;
#endif
#if 0
cout << MKSTR ( HELLO C++ ) << endl ;
#endif
cout << " The minimum value from the given input is " << MIN(x, y) << endl ;
#ifdef DEBUGGING
cerr << " We are moving out of the main function " << endl ;
#endif
return 0 ;
}
Output:
In the above code, you can see we have defined a MACRO with name DEBUGGING for checking the progress of our code while debugging and then we have declared one more MACRO as MIN to calculate the minimum from two integers given by the user. After that in the main class, we declared two integer variables x and y and assigned some values to them. In the output, you can see it will automatically start comparing the values when we use MIN ( a, b ) in our main function.
Example #2
Here is another C++ code to demonstrate the working of Macros in C++ programming:
Code:
#include <iostream>
// macro with parameter of Rectangle
#define AREA(l, b) (l * b) // defining the MACRO AREA for calculating area
int main ()
{
int length = 150 , breadth = 75 , area ;
area = AREA( length , breadth ) ;
std :: cout << " The Area of the Rectangle is : " << area ;
return 0 ;
}
Output:
In the above code, we are calculating the area of the rectangle by defining an AREA macro which takes length and breadth as the input from the user and then calculates the area. In the main class, we have declared two integer variables with the value assigned to them. Therefore, when in the main class we call AREA macro it will take the integer values as the input then will calculate according to the defined logic in the macro. In addition, the value will be calculated and then will be shown as output over the display screen.
Example #3
Here is another C++ code to demonstrate the working of Macros in C++ programming:
Code:
#include <iostream>
using namespace std ;
#define PI 3.14159 //defining MACRO PI to represent the universal value of PI
int main () {
cout << "The Value of PI is : " << PI << endl ;
return 0 ;
}
Output:
In the above code, we are calculating the area of the rectangle by defining a PI macro which represent the value PI every time a user calls it. In the main class, we called the MACRO PI therefore, in the output you can see the value of PI. As you can see we have added any logic in the PI value we have directly given in code.
Uses of Macros
Let us discuss the uses of macros in C++ programming and why MACROS are so important in making code efficient.
- MACROS definitely helps in converting complex lengthy codes into smaller as well as simpler codes.
- A name that defines its functionality and work, which is smaller and easy to understand as you have to use the defined name every time you need that functionality in your source code.
- Point to point declaration in one line without writing lengthy code.
- The programmer can decide whether to keep the macro after use or not as they won’t clean themselves even after the work is done. So a program can delete the MACRO anytime they want without making any changes in the code.
- Only one allocation is made at the time of declaration and that is the final one. No need to define any variable as it required memory allocation. As mentioned above just one time you have to let the compiler know that what is the MACRO function, it’s the name and the result.
Conclusion
Macros in C++ are the fundamentals that help in shortening a code if the same work if perform by two or more different codes. Therefore, it can also be used for polymorphism that will help in avoiding runtime overheads as they happen much earlier before the runtime.
Recommended Articles
This is a guide to Macros in C++. Here we discuss Introduction to Macros in C++and uses along with examples and its Code Implementation. You can also go through our other suggested articles to learn more –