From the course: C Programming for Embedded Applications

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Macro functions

Macro functions

- [Instruction] Here's another alternative to regular functions that has its subtle advantages and disadvantages. That's the macro function. This is another preprocessor trick, and it consists of using the #define directive. Here's an example. This definition does not simply replace the first part for the second. It actually causes the preprocessor to find all function calls for the macro, and replace them by the expression at the right with the appropriate argument. That's what inlining means. The function is copied in the code every time it's used. So this is an example, square macro function, and it's equivalent regular function definition. They both calculate the square of the argument. And as you can see, the syntax for using both functions is pretty much the same. So which is better? Let's see some advantages of the macro function over regular functions. A macro function runs a little faster, because being…

Contents