What are Macros in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

A C++ macro is defined as a section of code that that particular macro value can replace. We can define the macro by using a #define directive. When the compiler goes to the macros while program compilation, the macro's name is replaced by the definition of the macro. The termination of the C++ Macros does not need a semi-colon (;).

Given below is an example of the C++ macro:

Output:

Explanation: In the above code, we are computing the area of the rectangle by AREA macro that receives the user's input for the rectangle's length and width before calculating the area. Two integer variables with their respective values are declared in the main class. When the AREA macro is used in the main class, it will accept integer numbers as input and do the calculation following the macro's logic. Finally, the output gets printed out.

Types of Macros in C++

There are various types of C++ Macros. Let us discuss them one by one.

Chain Macros

As the name suggests, chain macros are defined as macros inside the macros. In the chain macro, the parent macro is expanded first, and then its child macros.

Given below is an example of the chain macro.

Output:

Object-Like Macros

The object-like macro is an identifier that gets replaced by code fragments. This macro is given the name object-like macro because the code segment looks like an object of code. This type of macro is generally used to replace a symbolic name with the variable being represented as a constant.

Given below is an example of the object-like macro.

Output:

Function-like Macros

The function-like macro is almost similar to the function call in a program. In it, the whole code is replaced by a function name. Since it is similar to function, there must be a pair of parentheses after the macro's name. We should never put a space between the macro name and the parentheses in the macro definition.

Given below is an example of the function-like C++ Macro.

Output:

Multi-Line Macros

A multi-line macro contains more than one line of definition in it. We create a multi-line macro by the use of backslash '\'.

Given below is an example of multi-line C++ Macros.

Output:

Common Preprocessor Functions

There are various preprocessor functions in C++ macros that we are going to discuss below.

#include:

This preprocessor function tells the system to include the current file specified in the argument. If the file that has to be included is not found, the compiler throws an error. This directive is of three types: 1.#include : It is for pre-defined header files in C++. If the header file is predefined, then write the header file name in angular brackets.

Example:

2.#include "file": It is for header files that the programmer defines. If a programmer has written their own header file, then write the header file name in quotes.

Example:

Here, mul.h is a header file written by the programmer.

#define:

This preprocessor function in C++ comes under Macros. A macro is one when the value of the macro substitutes some particular section of code. Various types of macros are:

  1. Object-like Macros: When the C++ Macros is replaced by a value, generally for numerical value.

Example:-

  1. Function-like Macros: When the macro id is replaced by a function call.

Example:

  1. Chain Macros: Macros inside macros are termed chain macros. In chain macros, first of all, the parent macro is expanded then the child macro is expanded.

  2. Multi-line Macros: An object-like C++ Macros could have a multi-line. So to create a multi-line macro, you have to use backslash-newline.

#undef:

This directive is used to remove the definitions related to a particular macro.

Syntax of "#undef" is as follows:

#ifdef:

This preprocessor function is used to check if a macro is defined earlier or not. If it is defined, then the code executes normally.

The syntax of this directive is as follows:

#ifndef:

This preprocessor function is used to check whether a "#define" is defined or not defined by the user. If it is so, then the code will execute.

The syntax of this directive is as follows:

More Examples to Understand Macros in C++

Let us see some more examples of C++ Macros.

  • Example 1.

Output:

Explanation: In this example, we first defined the macro square 16 and then used it to print in the code output using the cout.

  • Example 2.

Output:

Explanation: In the above example, we are getting the value of PI by defining a macro PI and its value which is 3.14159. The PI gets represented every time the user calls it.

Why to not Use #define for Constants?

There are various reasons we should not use the #define for constants in C++. Given below are some of the reasons.

  • No type checking: We can not type check the constant we define using the #define. So we must need to specify that type of constant. We can specify them like 3.4f.

  • No scoping: A constant that is defined in the #define is global.

  • No access control: We do not have any control over the #define.

  • No symbol: The preprocessor may remove the symbolic name from our code. The compiler never sees the name as a result and cannot add it to the symbol table. When we can only see the value of the constant and not the name, debugging is difficult.

To learn more about Preprocessor directives in C++, click here- Preprocessor Directives in C++

Conclusion

  • A C++ macro is defined as a section of code that can be replaced by that particular macro value.
  • The various types of C++ macros are Chain macros, Object-like macros, Function-like macros, and Multi-line macros.
  • The chain macros in C++ Macros are defined as macros inside the macros.
  • The object-like macro in C++ Macros is an identifier that gets replaced by code fragments.
  • The function-like macro is almost similar to the function call in a program.
  • A multi-line macro contains more than one line of macros in it.
  • The various preprocessor functions are #include, #define, #undef, #ifdef, etc.
  • There are also some reasons we should not use the C++ macros with the #define, such as No scoping, No type checking, No access control, and No symbol.