C Programming • C17 baseline, standards-specific features labeled
Advanced Macros and Variadic Functions
Use preprocessing and variadic functions sparingly, with attention to evaluation and format contracts.
A function-like macro performs token substitution and may evaluate an argument more than once. Variadic functions receive a variable number of arguments but cannot discover their types at runtime; they need a format string or another explicit convention.
Example
#include <stdarg.h>
#include <stdio.h>
void log_message(const char *format, ...) {
va_list arguments;
va_start(arguments, format);
vfprintf(stderr, format, arguments);
va_end(arguments);
}
What to expect
log_message("count=%d\n", 3) writes count=3 to the standard error stream.
Key idea: Keep format strings trustworthy, match each conversion with the correct argument type, and prefer inline functions over macros when possible.