O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C-Style Macros

In C programming, macros are short pieces of code. You can use these macros as a shorthand for C code, with or without parameters. This enables you to create short, function-like code snippets designed to fulfill a specific task. These can then be used in the code proper and can help to increase readability, while reducing the possibility of mistyping complex code segments.

One common implementation problem revolves around defining a function that’s capable of returning the smaller of two values. Let’s call this function min. There are several ways in which you can implement the min function:

  • Inline, as an if expression

  • As a function returning a value

  • As a macro

In the first case, you might write a condition test as follows:

int ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required