liveBook · Manning

Chapter 17: Function-like macros

What you will learn in chapter 17 of Modern C, Third Edition

The chapter introduction explains why inline functions are usually safer than function-like macros, while identifying when macros remain useful.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why inline functions are usually safer than function-like macros, while identifying when macros remain useful.

Key terms: function-like macro, inline function, type-safe interface, side effects, function-like macros, replacement string, simple interface

17.1 How function-like macros work

This section explains how function-like macros are expanded by the preprocessor and when a macro name is retained so it can refer to a function.

Key terms: textual replacement, preprocessing, early translation phase, function-like macro, argument list, parameters, replacement text, macro invocation, macro retention, function decay

17.2 Argument checking

This section shows how to use functions and function-like macros to catch argument errors that ordinary C function interfaces cannot always express.

Key terms: function-like macros, static, inline, argument checking, string literal, string_literal, function interface, fprintf, format string, TRACE_PRINT1

17.3 Accessing the context of invocation

This section shows how macros can capture information from their invocation site for debugging traces, including how to stringify a line number safely.

Key terms: context of invocation, __LINE__, __FILE__, __DATE__, __TIME__, __func__, int, unsigned long, fprintf, stringification

17.4 Variable-length argument lists

Introduces variable-length argument lists in functions and explains why their lack of type safety is a concern.

Key terms: variable-length argument lists, ..., type safe

17.4.1 Variadic macros

This section explains how variadic macros capture, forward, stringify, conditionally expand, and count variable argument lists, then uses those techniques in tracing macros.

Key terms: variadic macros, ..., __VA_ARGS__, stringified, __VA_OPT__, FPRINTF_II, FPRINTF_IIIplus, TRACE_VALUES, ALEN, compound literal

17.4.2 A detour: Variadic functions

This section explains how variadic function arguments are converted and accessed, and why their types and list length require explicit conventions.

Key terms: variadic parameter, float, double, char, short, variadic function, format argument, NULL, portability, <stdarg.h>

17.5 Default arguments

The section shows how function-like macros can supply commonly omitted arguments to C library conversion functions.

Key terms: default arguments, strtoul, strtod, endptr, base, __VA_OPT__, __VA_ARGS__, NPTR, _I, _II

17.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 16: Performance Chapter 18: Type-generic programming →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.