Skip to main content

Macro Expansion in C

Macro Expansion

  • Macro expansion in C is the process of replacing a macro with its corresponding value or code snippet in the source code during the preprocessing phase.

  • Macros are defined using the #define directive, which allows a programmer to define a sequence of code or a value as a macro name.

  • During macro expansion, the preprocessor reads the source code and looks for instances of the macro name.

  • When a macro name is found, the preprocessor replaces it with the corresponding value or code snippet defined in the macro definition.

  • The result is a modified source code that can be compiled or interpreted by the computer.

Syntax of Macro Expansion

The syntax of macro expansion in C programming language is as follows:

#define macro_name value

Explanation:

  • In this syntax, macro_name is the name of the macro being defined, and value is the value or code snippet associated with the macro.

  • The #define directive is used to define the macro, which is then expanded during the preprocessing phase of the compilation process.

Types of Macros

In C, there are two main types of macros:

  • object-like macros
  • function-like macros.

Object-like Macros

  • Object-like macros are simple text substitutions that replace an identifier with a specified value.

  • The syntax for defining an object-like macro is as follows:

#define macro_name value
  • Following macro definition creates an object-like macro called MAX that represents the maximum value of two numbers:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
  • When used in code, this macro expands to the maximum value of two numbers:
int x = 5;
int y = 7;
int max_value = MAX(x, y); // max_value will be 7

Function-like Macros

  • Function-like macros are more complex than object-like macros and can take arguments.

The syntax for defining a function-like macro is as follows:

#define macro_name(argument_list) replacement_text
  • Following macro definition creates a function-like macro called SQUARE that calculates the square of a number:
#define SQUARE(x) ((x)*(x))
  • When used in code, this macro expands to the square of a number:
int x = 5;
int square_value = SQUARE(x); // square_value will be 25

Example of Macro Expansion

#include <stdio.h>
#define PI 3.14159
#define SQUARE(x) ((x)*(x))

int main() {
double radius = 5.0;
double area = PI * SQUARE(radius);
printf("The area of the circle is %f\n", area);
return 0;
}

Explanation:

  • The #define directive is used to define two macros: PI and SQUARE. The PI macro is defined as the value of pi, and the SQUARE macro is defined as a code snippet that squares its argument.

  • During macro expansion, the preprocessor replaces PI with the value 3.14159 and SQUARE(radius) with ((radius)*(radius)).

  • The resulting code will look as below:

#include <stdio.h>

int main() {
double radius = 5.0;
double area = 3.14159 * ((radius)*(radius));
printf("The area of the circle is %f\n", area);
return 0;
}
  • As you can see, the PI macro has been replaced with its corresponding value of 3.14159, and the SQUARE macro has been replaced with its corresponding code snippet ((radius)*(radius)).

  • This results in a modified source code that can be compiled and executed by the computer.