C Programming

C Macros

Whenever a C code is compiled in the Linux operating system, it is submitted to such a compiler, which translates the C code into binary code before completing the compilation and running the C code. A macro preprocessor exists as another name for the C preprocessor. Throughout C, a macro is characterized as a sequence of code statements specified as a label and then cast off when that collection of code is needed. These macros often begin with the expression “#,” and the compiler executes the declarations that begin with such a sign. There are two kinds of C Macros which we will be discussing in today’s article as follows:

Object Like Macro: So if data structures have been jumble-sale, object-like macros will be cast-off.

Function Like Macro: The function-like macros have been cast off whenever method calls are being made.

Object Like Macro:

The object-like macro is a value-replaceable identifier. It’s a common way to describe numerical constants. So, Open your Linux operating system and log in from it. By the time of presenting the article, we have been using the Ubuntu 20.04 Linux system. After a quick login, launch the console shell via “Ctrl+Alt+T” or using the search bar from the Activity area.

Example 01:

So, Create a C type file “test.c” or whatever you want to name it via the GNU Nano Editor. This editor has been used to edit such files in the Linux operating system.

$ nano test.c

The file will be opened quickly. Include a library “stdio.h”. The code is presented in the picture. We have used the #define object like a macro for the variable “new” with a value “42”. We didn’t define it as we define a normal variable with the semicolon at the end of it. There is no need for “;” in macros. Now the code contains the “main” method to be executed. This main method has a single print statement within it. The print statement is showing the result of the variable “new” with some string in it. Save your updated file and return to the terminal via “Ctrl+S” and “Ctrl+X” correspondingly.

Before the execution of the file, the code needs to be compiled first. Hence make sure you have any C language compiler set up in your Linux system. By the time of writing this guide, we have been utilizing the “gcc” compiler. If you want to install it as well, then try the below query in your console.

$ sudo apt install gcc

After installing the compiler, we will compile the code with the “gcc” query. This query must contain the file name to be compiled as below.

$ gcc test.c

Now the compilation is successful and gives no errors; we will run our file via “a.out” instruction like stated underneath. The output has presented in the image showing the value of a macro variable.

$ ./a.out

Example 02:

Now we have the next example of the object-like macro. This example will be a little dissimilar from the previous one. So open the same file to update our lines of code.

$ nano test.c

We have defined a variable “val” with float value “5.765” in it as an object-like macro at the top of the code after the library. Within the main function, two float type variables, “r” and “a,” have been initialized with no value at the time of initialization. The variable “r” will be used as radius, and variable “a” will be used as “area.” The print statements will show a message to a user to enter a radius of his/her own choice. The scanf line has been used to get the input from the user via terminal. This value entered by the user will be considered as a float value, and it will be bound to the variable “r.” We have been calculating area “a” by calculating the object-like macro variable and radius entered by a user on the next line. After that, the calculated area will be displayed on the screen via a print statement.

Compile the document code with “gcc.”

$ gcc test.c

Run the file and enter the radius upon request, and it will calculate the area for your entered value.

$ ./a.out

Function Like Macro:

In the Function Like Macro, we will define a function instead of any variable. So, let’s get started.

Example 01:

Open the C document test.c to change the code.

$ nano test.c

The function “merge” is used as a macro with two variables in its parameter. You have to define the logic of a function while using the #define macro function because your machine doesn’t understand without it. So, we have defined “a##b.” The main method will show the merge of two integer type values passed in argument to the merge function in the terminal via the print statement.

Compilation could be done with the “gcc” keyword.

$ gcc test.c

When you execute the “test.c” file, you will get the merged value of both the two integer type values, which has been passed in the print statement to the merge method.

$ ./a.out

Example 02:

Let’s have a last example of a function like macros. This time we have been printing a string value with some limit defined. Open “test.c” C file.

$ nano test.c

We have implemented a MACRO function in which “a” variable represents the start value, and “lim” represents the end of a limit. While the “a” is less than “lim,” it will print the string “Aqsa” and increment variable “a.” The main method contains the initial value of “a,” and the MACRO method has an “8” value passed in it to the “lim” variable, so it must print the string “8” times.

Compile via:

gcc test.c

Execution of code is printing the string “Aqsa” 8 times.

$ ./a.out

Conclusion:

We have covered both the object-like macros and function-like macros in this guide. We hope it will fade away all your doubts and clear your mind about C macros.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.