How C macros Really work – a sneak peek

Reading Time: 4 minutes

If you have taken the slightest hands-on experience with the C programming language, you probably should have used macros in one way or the other. Today let’s find out how C macros really work by understanding what goes on behind the scenes during compilation. If you are new to C, I suggest you check this article first.

What are we going to learn today?

  • Write and Compile a C program using a normal text editor (Sublime Text)
  • View the program source code with expanded macros (this intermediate file is not visible when using IDEs)
  • Run the program using Command prompt

Full source code for today’s lesson

#define square(num) ((num)*(num))
#include <stdio.h>

void main(){
	int answer = square(3);
	printf("%d", answer);
}

Code explained

Line 1 is the macro definition. A macro definition has 3 parts.

macro
  • Preprocessor Directive
  • Macro Name
  • Macro Output

Line 2 includes the stdio.h header file which contains the printf() library function (among many other useful library functions).

In Line 5, the macro is invoked. This is like calling a function in C. The argument for the Macro Parameter num is sent as 3 in the given example.

The output value of the macro can be thought of like a return value of a normal function in C. This value is put to the variable answer in line 5.

In summary, what happens is, we invoke the macro with the argument 3; The macro outputs the square value of that number 3; That output is saved in the variable answer.

Then the value of the variable answer is output to the screen using the printf() function.

It’s generally a good practice to include the Macro Parameters within parenthesis always in the Macro Output, to avoid unexpected errors in the code. For example, ((num) * (num)). Otherwise, problems related to operator precedence will occur when these Macro Parameters are Expressions instead of simple variables.

Behind the scenes

When we compile a C program using an IDE like CLion or CodeBlocks, the preprocessing part is hidden from us. It is here in the preprocessing part that the macros are “expanded”. So when we use such an IDE, we don’t see the intermediate file created by expanding the macros.

Thus, in order to view the expanded macro parts, we have to use a normal text editor along with command prompt (in windows).

Steps

Step 1

Write the C program using a text editor. We have used Sublime Text editor for writing code. But any code editor will work.

Step 2

Locate to the folder where main.c (our source file) is located from command prompt, and type the following command and hit enter.

gcc -E main.c

Scroll the command prompt to the bottom and you should see an output like this.

This is our program but with the macros expanded!

gcc is the name of the compiler we are using.

-E is called a flag and it is used to signal to the compiler to output the macro-expanded version of our source code.

Here, main.c is the name of the source code file in which we wrote our C program. This name could be anything.

Step 3

Compile the program to create a .exe executable file using the command,

gcc main.c
Step 4

Execute our program using the command,

a

You should now see the output of our program printed onto the command prompt as 9, which is infact the square of 3.

Here a is the name of the executable file that was created by the compilation process. We can change the name of this file when compiling, but to keep things simple, let’s use this way.

Conclusion

And that’s how the macros really work in C when we are compiling a C program.

Macros are fast and efficient than normal functions due to the fact that they are pre-processed, meaning they are processed before compiling the code. These macros act like substitutions for values. There are many uses of macros which compete with functions, but that’s a topic for another day!

Like rukbook.com so far? Chances are, you’ll like rukbook on YouTube (coming soon)! Click the button below to stay subscribed ahead of time!

Did you enjoy this article? Why not share it with your friends and connections? It helps spread the word about rukbook and it means a lot! Thanks for reading. Subscribe to rukbook.com mailing list by just entering your email below! It’s just one click!

Subscribe

* indicates required
5 2 votes
Article Rating

Leave a comment. We love your feedback.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Magi
Magi
1 month ago

Normally Macros are used for small and frequently used codes, if the code is long we have to go for a normal function, isn’t it?

Stark
Stark
23 days ago

This article cleared some doubts. Thank You. Amazing job ! Keep going !

4
0
Would love your thoughts, please comment.x
()
x