Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What are Macros?
Defining Macros
Macro Functions
Benefits and Drawbacks of Macros
Frequently Asked Questions
What are Macros in C++?
How to define a Macro?
Can Macros be used to define functions?
Last Updated: Mar 27, 2024

Macros in CPP

Author Gunjan Batra
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


One of the fascinating features of C++ that adds to its flexibility and power is the concept of macros. Macros can save you time, simplify complex commands, and can even serve as a form of in-line function creation.

macros in C++

Let's dive in and understand more about macros in C++.

What are Macros?

Macros are a powerful feature of the C++ preprocessor. The preprocessor is a tool that manipulates text data before the compilation process begins. Macros are, in essence, a set of instructions that are given a name. Once defined, the macro can then be used to insert these instructions into your code.

Defining Macros

Macros are defined using the #define directive. The general form is as follows:

#define identifier replacement

The identifier is the name of the macro, and replacement is the set of instructions the preprocessor will insert whenever it encounters the identifier.

For example:

#define PI 3.14159

Here, PI is the identifier, and 3.14159 is the replacement. Now, whenever PI is used in your code, the preprocessor will replace it with 3.14159.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Macro Functions

Macros can also be used to define functions. Here's an example:

#define SQUARE(X) ((X) * (X))

In this case, the identifier SQUARE(X) behaves like a function. The replacement ((X) * (X)) will be inserted wherever SQUARE(X) is used.

Let's see this in action:

#include <iostream>
#define SQUARE(X) ((X) * (X))

int main() {
    int num = 5;
    std::cout << "Square of " << num << " is " << SQUARE(num);
    return 0;



In this code, SQUARE(num) is replaced by ((num) * (num)), and the result is printed to the console.

Also read -  File Handling in CPP

Benefits and Drawbacks of Macros

Macros can be powerful, but they should be used judiciously.


  • Macros are processed before compilation, so they can reduce execution time.
  • Macros can simplify complex code or mathematical formulas.


  • Macros are not type-checked, which can lead to errors.
  • Macros don't have a scope, they're global, which can cause naming collisions.

Also see, Abstract Data Types in C++

Frequently Asked Questions

What are Macros in C++?

Macros are a set of instructions that are processed by the preprocessor before the actual compilation begins.

How to define a Macro?

Macros are defined using the #define directive followed by an identifier and its replacement.

Can Macros be used to define functions?

Yes, Macros can be used to define functions, which is done by using the identifier as the function name and the replacement as the function body.


Macros in C++ offer a powerful way to define reusable sets of instructions, simplifying complex commands, and reducing execution time. However, they should be used with caution due to their lack of type-checking and global scope. As with all tools, understanding their strengths and weaknesses is key to using them effectively.

Recommended articles:

We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Happy Learning!! 

Previous article
Top 20 reasons to learn C++
Next article
How to Download and Install C++ IDE on Windows
Live masterclass