Macros with arguments let you create reusable code pieces that work like small functions but run faster because they replace code directly.
Macro with arguments
#define MACRO_NAME(argument1, argument2, ...) (expression using arguments)
Macros are replaced by the preprocessor before the program runs.
Arguments in macros are like placeholders that get replaced with actual values.
#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ADD(x, y) ((x) + (y))
This program uses a macro with arguments to multiply two numbers and print the result.
#include <stdio.h> #define MULTIPLY(a, b) ((a) * (b)) int main() { int x = 5; int y = 3; int result = MULTIPLY(x, y); printf("%d times %d is %d\n", x, y, result); return 0; }
Always use parentheses around macro arguments and the whole expression to avoid unexpected results.
Macros do not check types, so be careful with side effects like incrementing variables inside arguments.
Macros are replaced before compilation, so debugging can be harder than with functions.
Macros with arguments let you write reusable code snippets that work like simple functions.
They are replaced directly in your code before it runs, making them fast but sometimes tricky.
Use parentheses carefully to avoid mistakes in calculations.