How to Create a Macro in C

Embedded C for microcontrollers

In this article, we go over how to create a macro in C.

A macro is a preprocessor directive which allows a programmer to make a textual replacement for some value in code.

A very simple example would be a program that calculates a mathematical function such as the area of circle, using PI.

Instead of using the value of PI for each of the area calculations, we can define PI in a preprocessor directive as a text with a value and then use this text throughout the program to represent the value of PI.

So we show how to do this in the program below.

Let's now review this code.

So you see we have defined a macro named PI at the top of our code before the main function.

This is a preprocessor directive which tells the compiler the value of this macro, which can later be substituted into any part of our program.

Notice how semicolons are not used to define macros.

One advantage of using macros is that you define the value once in code and can use it throughout the code. If ever the value changes, for example, with PI if you wanted to define it more precisely by adding more digits, you would just need it to change that value one place in code instead of several.

Macros are also useful for making code much more readable.

With certain codes such as when working with embedded microcontrollers, using macros can make code much more understandable and clearer.

So you can see at the beginning we define 2 macros in order to turn on the peripheral clock registers for GPIO PORTA and PORTD.

We then define another macro that sets pin 12 of PORTD as output and another that sets pin 0 of PORTA as input.

Lastly, we create another macro that allows us to change the output state of pin 12 of PORTD, either turning it on or turning it off.

Notice how these macros help to make the code more understandable and clearer.

There's one more thing we can do with macros.

Other than just use them purely as a textual replacement for a value, we can create function-like macros, which define macros that act as functions.

Let's create a function in which a user inputs a person's weight in pounds (lbs) and outputs the weight as kilograms (kg).

The program below create a macro-like function in C that does this.

So we define a function, named weightkgs(), which takes in a single parameter, the weight of a person in pounds. The function then returns the weight of the person in kilograms.

So just as you can define regular values with macros, you can do the same for functions and use these functions within your code.

So, in summary with macxros, it is best to use meaningful names when creating them, use upper case letters as a convention to distinguish them from variables.

It is important to know that macros are not variables. They do not consume any ram space during compile time of a program.

When using function-like macros, make sure to surround all values with parenthesis. This is best practice.

And this is how to create a macro in C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C

HTML Comment Box is loading comments...