What are Macros in C++?

In C++, a macro is defined as the section of the code that contains the macro value that can be replaced. We can define the macro keyword with the help of the #define directive. During the program compilation, the compiler goes to the macros then the macro name changes to the macros. There is no need for semi column(;) for the termination of the macros.

You can also create macros that take arguments using the #define directive followed by the name of the macro, its arguments in parentheses, and the replacement text. For example:

This creates a macro named "SQUARE" that takes one argument, "x," and returns the square of that argument. You can use this macro in your code like this:

Note that the preprocessor expands macros before the code is compiled and can sometimes lead to unexpected behavior or errors if not used carefully. It's important to keep in mind the potential pitfalls of macros, such as unintended side effects or issues with operator precedence.

Example 1:

Output:

What are Macros in C++

Explanation:

In the above program, we are going to compute the area of the rectangle with the help of the area of macros that receives the length and breadth of the rectangle as the user input value. There are two integer values that declare their respective value in the main class of the program. The user input accepts the integer value and calculates the area of the rectangle area by the help of the logic of the macro.

Types of Macros in C++

In C++, there are so many types of macros available. Let's discuss each one by one.

1. Chain macros:

In C++, chain macros are the type of macros that allow the programmer to concatenate the multiple string or arguments together into a single expression. These chain macros are very useful for complex macros that generate code based on multiple inputs.

We can create the macro chain with the help of the ##operator. We can call this operator a token-pasting operator. With the help of this operator, concatenate two operands into a single token.

The micro can take two arguments, a and b, and concatenate themselves with the help of ##operater. For example, if you use the macro like this:

You can also use chain macros to create more complex expressions or statements.

For example:

This macro takes an expression "expr" and a message string "message" and generates a simple assert statement. The ## operator is used to concatenate the message string to the end of the printf statement. For example, if you use the macro like this:

Chain macros can be a powerful tool for creating reusable code and reducing duplication. Still, they can also make code more difficult to read and debug if used excessively or improperly. It's important to use them judiciously and keep in mind their potential downsides.

Example 2:

Output:

What are Macros in C++

2. An object like macros:

In C++, macros are used to define a set of instructions that are executed when the macro is called. Object-like macros are a type of macro in C++ that acts as a simple text substitution.

In an object-like macro, a specific identifier is assigned a value, which can be a literal value, an expression, or another identifier. Whenever the identifier is encountered in the code, it is replaced by the value assigned to it.

Here is an example of an object-like macro:

In this example, PI is the identifier and is assigned the value of 3.14159. Anytime PI appears in the code; it will be replaced by 3.14159.

Object-like macros are often used to define constants or to simplify code by replacing a long-expression with a shorter identifier. However, they can also be a source of bugs if not used carefully, as they can change the meaning of code in unexpected ways.

It's important to note that object-like macros are a preprocessor feature, which means that they are evaluated before the code is compiled. This can lead to unexpected behavior if the macro is used in a context where it doesn't make sense, such as within a string or a comment. To avoid these issues, it's a good practice to use const or enum instead of macros to define constants, as they are evaluated by the compiler and offer better type safety.

Example:

Output:

What are Macros in C++

3. Function like macros:

Function-like macros in C++ are a type of macro that resemble functions but are a preprocessor feature that provides a text substitution mechanism. A function-like macro can take arguments, and when called, its arguments are replaced by their respective values, which are then substituted in the macro's body.

Here is an example of a function-like macro:

SQUARE is the macro name in this example, and it takes a single argument x. The macro body is ((x) * (x)), which returns the square of x. When this macro is used in the code, for example:

the preprocessor replaces SQUARE(3) with ((3) * (3)), which becomes nine after the code is compiled.

Function-like macros can also have multiple arguments, for example:

This macro takes two arguments, a and b, and returns the maximum value between them.

However, function-like macros can be a source of bugs if not used carefully, as they can change the meaning of code in unexpected ways. One common issue is that macro arguments are not type-checked, which can lead to unexpected results if the arguments have side effects or are evaluated multiple times.

Therefore, it's a good practice to use function-like macros only when necessary and to use function templates instead when possible, which provide a more type-safe and reliable alternative.

Example:

Output:

What are Macros in C++

4. Multiline macros:

Multiline macros in C++ are a type of macro that allows the definition of more than one line of code. This is useful when the macro definition requires multiple statements or when the definition is too long to fit on a single line.

Here is an example of a multiline macro:

In this example, DEBUG_LOG is the macro name, and it takes a single argument msg. The macro body contains multiple lines of code, which are separated by the \ character. This character is used to indicate that the macro definition continues on the next line.

When the macro is used in the code, for example:

which prints a debug message to the console.

It's important to note that multiline macros can also be a source of bugs if not used carefully, as they can change the meaning of code in unexpected ways. It's important to use consistent indentation and to wrap all macro arguments and code lines in parentheses to avoid unexpected operator precedence issues.

Also, it's generally recommended to use inline functions instead of macros for complex code, as they offer better type safety and are easier to debug. However, multiline macros can still be useful for simpler code, such as debug statements or simple utility functions.

Example:

Output:

What are Macros in C++

Common Preprocessor Functions

There are so many preprocessors available in the C++ macros. We are going to discuss those below.

1. #include:

In C++, #include is a preprocessor directive that is used to include the contents of a header file into the source code. When the preprocessor encounters an #include directive, it replaces the directive with the contents of the specified file before the compiler starts compiling the code.

Macros, on the other hand, are a way to define symbolic constants or small pieces of code that can be expanded by the preprocessor into larger pieces of code. Macros are defined using the #define directive and are often used to simplify code or to make it more readable.

In some cases, it can be useful to use macros in combination with #include. For example, you could define a macro that includes a particular header file and then uses that macro in multiple places throughout your code. This can make it easier to modify the included files in the future, as you only need to change the macro definition instead of updating each individual #include directive.

Here's an example of using a macro with #include:

In this example, the macro MY_HEADER is defined as the string "my_header.h", and then the #include directive uses the MY_HEADER macro to specify which header file to include. This is equivalent to writing #include "my_header.h" directly, but using a macro can make the code more modular and easier to maintain.

2. #define:

In C++, #define is a preprocessor directive that is used to create macros, which are a way to define symbolic constants or small pieces of code that can be expanded by the preprocessor into larger pieces of code. Macros can be used to simplify code, make it more readable, or avoid repeating code.

A macro definition consists of the #define directive followed by the name of the macro and the value or code that the macro represents. The syntax of the #define directive is as follows:

Here, macro_name is the name of the macro, and replacement_text is the value or code that the macro represents.

It's important to note that macros are expanded by the preprocessor before the code is compiled. This means that macros are not part of the compiled code, and they cannot be debugged like regular code. Additionally, macros can sometimes make the code harder to read and understand, so it's important to use them judiciously and to choose meaningful names for the macros.

3. #undef:

In C++, #undef is a preprocessor directive that is used to remove a macro definition. The syntax of the #undef directive is as follows:

Here, macro_name is the name of the macro that you want to remove.

The #undef directive is useful when you want to remove a macro definition that you no longer need or when you want to redefine a macro with a different value or code.

It's important to note that macros are expanded by the preprocessor before the code is compiled. This means that the effects of the #undef directive are only visible during the preprocessor phase and not during the actual compilation of the code. Additionally, removing a macro definition that is still being used in the code can cause compilation errors, so it's important to be careful when using #undef.