liveBook · Manning

Chapter 18: Type-generic programming

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

The introduction shows that C already has widespread type-generic features, then contrasts the fixed-type limits of _Generic with C23 type inference.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction shows that C already has widespread type-generic features, then contrasts the fixed-type limits of _Generic with C23 type inference.

Key terms: type-genericity, operators, type promotions and conversions, macros, variadic functions, function pointers, _Generic, generic selection, compile time, fixed set of types

18.1 Inherent type-generic features in C

Introduces a selective discussion of C’s inherent type-generic features and the issues it will highlight.

18.1.1 Operators

This subsection explains how C operators provide type-generic expressions and how they handle operand types.

Key terms: type-generic, operators, conversions, shift operators, object pointer addition, array subscripting

18.1.2 Default promotions and conversions

This subsection explains how C converts arithmetic values to required types and chooses a common type for binary arithmetic.

Key terms: conversion, argument type, parameter type, promotion, default argument conversion, variadic function, float, double, default arithmetic conversion, binary arithmetic operators

18.1.3 Macros

This subsection shows how C macros can support type-generic expressions, declarations, and statement groups, and explains the tradeoffs of statement-like macros.

Key terms: preprocessor, function-like macros, default arithmetic conversion, type-generic expressions, type declarations, definitions, macro invocation, do ... while(false), auxiliary variables, macro parameters

18.1.4 Variadic functions

This subsection explains how variadic functions provide type-generic interfaces, while highlighting argument conversions and checking limits.

Key terms: variadic functions, ..., type-generic interfaces, narrow integers, float, format string, user-specified functions

18.1.5 Function pointers

This section explains how function pointers let algorithms such as qsort and bsearch work with different types by accepting a comparison function.

Key terms: function pointers, compar, qsort, bsearch

18.1.6 void pointers

This subsection explains how void-pointer parameters make comparison interfaces type-generic, while leaving type safety to the user.

Key terms: void pointers, qsort, bsearch, BASE, target qualification, type safety

18.1.7 Type-generic C library functions

This subsection shows how C library type-generic interfaces select type-appropriate behavior, preserve pointer const qualification, and sometimes require more than size-based selection.

Key terms: <tgmath.h>, type-generic macro, cos, <math.h>, <complex.h>, C23, const-contract, const-qualified, function interface, <stdatomic.h>

18.2 Generic selection

This section explains how C’s _Generic selects expressions by type, how to use it in type-generic interfaces, and the constraints that make those selections valid.

Key terms: _Generic, controlling expression, default, function pointer, type-generic macro, min, argument promotions, conversion, result type, ternary operator

18.3 Type inference

The section introduces C23 type inference as a way to avoid handling a combinatorial number of type cases.

Key terms: type inference, expression, type name, combinatorial explosion

18.3.1 The auto feature

This section explains how C23’s auto infers variable types from initializers and why that helps type-generic macros and type-consistent local declarations.

Key terms: auto, initializer, SWAP macro, swap_p1, swap_p2, swap_tmp, do ... while(false), macro parameters, evaluated once, swap-prefixed names

18.3.2 The typeof feature

Explains when typeof and typeof_unqual are useful beyond auto, especially for type-generic macros, and shows the complexity and tradeoffs of combining them with _Generic.

Key terms: auto, typeof, qualifiers, generic selection, type compatibility, SWAP, _Generic, pown, pow, multiparameter type matching

18.4 Anonymous functions

The section compares compiler extensions for expression-like macros and uses a maximum macro to show why single evaluation and mixed signedness matter.

Key terms: do { ... } while(false), compound expression, void expression, block closure, lambda, captures, MAX macro, signedness, implicit integer conversions, UINT_MAX

18.5 Summary

Concepts explained in this chapter

← Chapter 17: Function-like macros Chapter 19: Variations in control flow →

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.