5.2.1. Macros with Arguments

A simple macro always stands for exactly the same text, each time it is used. Macros can be more exible when they accept arguments. Arguments are fragments of code that you supply each time the macro is used. These fragments are included in the expansion of the macro according to the directions in the macro definition. A macro that accepts arguments is called a function-like macro because the syntax for using it looks like a function call.

To de_ne a macro that uses arguments, you write a `#define’ directive with a list of argument names in parentheses after the name of the macro. The argument names may be any valid C identifiers, separated by commas and optionally whitespace. The open-parenthesis must follow the macro name immediately, with no space in between.

For example, here is a macro that computes the minimum of two numeric values, as it is defined in many C programs:

#define min(X, Y) ((X) < (Y) ? (X) : (Y)) then inside your function you write the name of the macro followed by a list of actual arguments in parentheses, separated by commas like this:

Ret_type func_name(){

min(1,2); // or min (x + 28, *p) or any argument you wish

}

Leave a comment