You can define macros with arguments using the #define
directive. Here's an example:
#include <iostream>
#define MULTIPLY(a, b) ((a) * (b))
int main() {
std::cout << MULTIPLY(3, 4) << "\n";
std::cout << MULTIPLY(2 + 3, 4 + 5) << "\n";
}
12
45
When defining macros with arguments, it's important to surround the arguments with parentheses to ensure proper precedence. This prevents unexpected behavior when the macro is expanded with complex expressions as arguments.
Answers to questions are automatically generated and may not have been reviewed.
Learn the fundamentals of the C++ build process, including the roles of the preprocessor, compiler, and linker.