liveBook · Manning

Default value

As explained in Modern C, Third Edition

A default argument is a value supplied automatically when a caller omits an otherwise optional function argument.

Defaulting strtoul arguments

A wrapper can let callers omit endptr and base: strtoul(argv[1]) expands to strtoul(argv[1], nullptr, 0), while strtoul(argv[1], ptr, 10) remains unchanged. section 17.5

C functions do not receive omitted arguments directly. Function-like macros simulate defaults by counting supplied arguments, selecting a specialized macro, and inserting missing values before the function call. section 17.5

The implementation pattern separates argument handling from the wrapped function:

  • Use __VA_OPT__ to distinguish an empty optional argument list from a nonempty one. section 17.5
  • Dispatch to fixed-arity helpers such as DEFAULT1, DEFAULT2, or DEFAULT3, which substitute defaults and reject excess arguments. section 17.5
  • Prefer a simple macro interface over exposing the complex replacement logic. section 17.0

How Modern C, Third Edition covers it

How deep it goes
Explains dispatch, default insertion, function wrapping, and excess-argument errors, but stops short of general language-level defaults.
Examples
Demonstrates strtod and strtoul wrappers, DEFAULT1 expansions, and CALL3 substitution traces.
What it assumes
Assumes familiarity with C function calls, variadic macros, macro expansion, and the __VA_OPT__ construct.
What you can do after
Apply it via C preprocessor macros to wrap functions with omitted arguments and compile-time arity checking.
How it connects
Connects default arguments with variadic macros, __VA_OPT__, function-like macros, and inline functions.

Also explained in

Compare how 20 books explain default value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.