c / expert
Snippet
Variadic Macros with __VA_ARGS__
The __VA_ARGS__ identifier is used in the macro expansion to represent the variable arguments passed to the macro. This allows creating powerful wrappers around functions like printf while automatically injecting metadata like file names and line numbers.
snippet.c
1
2
3
4
5
6
7
8
9
#include <stdio.h>#define DEBUG_LOG(fmt, ...) \fprintf(stderr, "[DEBUG] %s:%d: " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)int main() {DEBUG_LOG("Processing %d items", 42);return 0;}
Breakdown
1
#define DEBUG_LOG(fmt, ...)
Defines a macro that accepts a format string and a variable number of arguments.
2
__VA_ARGS__
Special macro identifier that expands to the additional arguments provided.