What are function-like macros in C?

In C programming language, a macro is a preprocessor directive that replaces text in the source code with a specified value.

Function-like macros

These types of macros are essentially preprocessor directives that behave like C functions.

They can be used to define short functions that can be used in the code just like regular functions by using the function name and providing an argument.

Syntax

Below, we can see the syntax of defining a function like macro.

#define MACRO_NAME(argument_list) (replacement_text)
Function-like macro syntax

Here is a breakdown of the syntax:

  1. To define a function-like macro, we will use the #define keyword followed by the name of the macro MACRO_NAME , which will be the name of our function.

  2. Next, we will provide a list of arguments argument_list to the macro function enclosed in () brackets. A comma will separate each argument.

  3. In the replacement_text, we will define what we want our function to return.

Note: When a function-like macro is used in our code, it is replaced with the replacement text. So MACRO_NAME(argument_list) gets replaced with (replacement_text) by the preprocessor.

Example

Let's say we want to make a function-like macro that converts a value from radian to degrees. We name this macro RADTODEG.

We must provide the function with an argument representing the radian value to be converted. We will call this argument RAD_Value.

Now, we need to define what the function should return. We want to multiply the argument RAD_Value with 57.2958 to get its equivalent degree value. So our replacement_text would be (RAD_Value * 57.2958).

So our macro definition will look like the code shown below.

#define RADTODEG(RAD_Value) (RAD_Value * 57.2958)

Code example

Below is a C code example showing how to use the macro we defined above.

#include <stdio.h>
#include <unistd.h>
#define RADTODEG(RAD_Value) (RAD_Value * 57.2958)
int main(){
// define radian variable
int rad = 3;
// get degree value using macro
double deg = RADTODEG(rad); // (rad * 57.2958) will be replaced here
// display result
printf("For radian value: %d, degree value is: %f", rad, deg);
return 0;
}

Code explanation

  • Line 4: We define a RADTODEG macro that takes in an argument RAD_Value and performs the replacement RAD_Value * 57.2958.

  • Line 8: We create a rad variable representing a radian value and initialize it with the value 3.

  • Line 10: Here, we create a deg variable and use the macro RADTODEG to retrieve the equivalent degree value of the radian variable.

  • Line 13: We print the values that we received onto the console.

Coding exercise

We have now understood what function-like macros are and how they are defined and used in our C code. We will now try to implement the concepts we learned by solving the challenge below.

Challenge

We want to make a macro SQUARE that takes an argument and returns the square of the value in the argument.

Edit the macro definition at line 4 so we get the squared value when we use the macro.

#include <stdio.h>
#include <unistd.h>
#define // define macro here
int main(){
// define the starting value
double value = 3;
// get squared value using macro
double squared_value = SQUARE(value);
// display result
printf("The square of: %d, is: %d", value, squared_value);
return 0;
}

Copyright ©2024 Educative, Inc. All rights reserved