In C programming language, a macro is a preprocessor directive that replaces text in the source code with a specified value.
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.
Below, we can see the syntax of defining a function like macro.
#define MACRO_NAME(argument_list) (replacement_text)
Here is a breakdown of the syntax:
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.
Next, we will provide a list of arguments argument_list
to the macro function enclosed in ()
brackets. A comma will separate each argument.
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.
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)
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 variableint rad = 3;// get degree value using macrodouble deg = RADTODEG(rad); // (rad * 57.2958) will be replaced here// display resultprintf("For radian value: %d, degree value is: %f", rad, deg);return 0;}
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.
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.
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 hereint main(){// define the starting valuedouble value = 3;// get squared value using macrodouble squared_value = SQUARE(value);// display resultprintf("The square of: %d, is: %d", value, squared_value);return 0;}