C Programming

Macros in C

“As you know that C is quite a diverse general-purpose language and came up with many different structures. The Macros in C language come in handy when swapping your large code with a single piece of line code to avoid complexity and more. The macros can be utilized in the C codes using the “#define” keyword. Although macros have many types, we will demonstrate the use of object-like macros and function-like macros in this article. Let’s have a fresh start on this guide by updating it in the shell by the “update” query shown in the attached image. Provide this query with your Linux password to continue the processing.”

Now, configure the gcc utility of Linux for the compilation of your C codes in the just coming examples. For this, use the “apt” utility with the “install” query and the “gcc” keyword.

Now, you need a C code file in which you will put some C code to play with different inputs and outputs. Thus, we recommend trying the “Touch” instruction in your Linux shell for any type of file creation. Use the “.c” extension at the end of a file to identify C. Your newly generated “macro.c” file would be saved to the Linux current home folder.

Example 01

Open your new C file to create our first example for object-like macros in it. You can use any editor by choice. If you are an expert C developer, it is recommended to use the “Gnu Nano” editor and if you are just a beginner, try the “text” editor. Both editors are quite appealing and easy to use for beginners. Within this code file, we are adding the “#include” keyword with the “<stdio.h>” C header in the beginning as it is a C library to use standard inputs and get standard outputs using simple C functions.

Now, we will define a macro just like an object using the “define” keyword. This object-like macro is used to initialize a variable named “val” with a value of 14. We didn’t add any “;” characters after the initialization statement because this is how the macros work and doesn’t require the same full-fledged structure of variables or functions. This macro variable can be utilized anywhere in the code. We are starting our main() function for execution with the “printf()” function. This printf() function simply displays a string type value along with using the macro variable “val” to display its value.

We will save this simple and basic code for object-like macro utilization in C.

#include <stdio.h>

#define val 14

int main() {

  printf("Value defined as Macro: %d\n", val);

}

After the file saving, its compilation is a must when you are working with general-purpose languages like C. Therefore, for the compilation of just completed C code, we are casting off the “gcc” command with the file name “macro.c”. This command will compile the code within the macro.c file and display the errors; if any. Now that our code is fully compiled and without errors, we are casting off the “./a.out” instruction at the console to run the compiled code. The output of this C code lets us display the string message along with a value of an object-like macro variable, i.e., displayed the value 14 for object-like variable “val”.

Example 02

Here comes the illustration of a function-like macro in which we will define a macro function that works the same as a function with no return types. Thus, we have been defining a total of 3 function-like macros after the stdio.h header in the shown-below C code. All three macros perform different operations on “x” and “y” variable values, which will be passed by the main() function, i.e., sum, multiply and subtract. Two integer variables, v1, and v2, are initialized in the main() method, and three printf() statements are used to display the calculated results by calling the function-like macros.

The sum() macro will calculate a sum using the logic (x+y), the mul() function-like macro will calculate the multiplication result using the logic (x*y), and the sub() function-like macro will subtract the two variables using the logic (x-y). That’s it for the three function-like macros; we will save them before compilation.

#include <stdio.h>

#define sum(x, y) x+y

#define mul(x, y) x*y

#define sub(x, y) x-y

int main() {

  int v1 = 7, v2 = 14;

  printf("Sum: %d\n", sum(v1, v2));

  printf("Multiply: %d\n", mul(v1, v2));

  printf("Subtract: %d\n", sub(v1, v2));

}

After the compilation and running of the C code, the output successfully displayed the sum, multiply, and subtraction result using the function-like macros.

Example 03

Let’s take a look at the last example of function-like macros. We defined a macro function taking two variables, “v” and “l” and using the while loop to execute the “{}” part of a code, i.e., until the value of “v” is less than “l”. The “{}” part will continue to print “Macro” at the shell using the printf() until the value “v” is equal to the value “l” using the increment on “v”. The main() function has been initializing the variable “v” with value “0”. The call to function-like macro uses the variable “v” along with passing the value “10” for the “l” variable to define a limit for a “loop” structure. Now, the print() statement in the function-like macro would continue to display “Macro” on the output screen until the variable “v” reaches “10”.

#include <stdio.h>

#define MACRO(v, l) while (v < l) \

{ \

  printf("Macro "); \

  v++; \

}

int main() {

  int v1 = 0;

  MACRO(v, 10);

}

It’s time to save the C code, compile and run it as we did in the image below. The output of this new C code shows the display of the word “Macro” 10 times on repeat. This is because we used the “while” loop to continue displaying it until “V” reaches “L” i.e., L=10.

Conclusion

This guide has explained the purpose of using the macros in C within the introductory paragraph and some very simple examples to demonstrate that. We have tried to use the object-like macros and the function-like macros within the three consecutive C examples and hope these examples will greatly help C developers.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content