可変長引数のマクロ

__VA_ARGS__で可変長引数のマクロを実現できる。

example

#include <stdio.h>

#define DEBUG_PRINT(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)

#define DEBUG_PRINT2(...) fprintf(__VA_ARGS__, 22)

int main(int argc, char **argv) {
  char *name = "Taro";
  int id = 22;
  DEBUG_PRINT("name: %s, id: %d\n", name, id);
  DEBUG_PRINT2(stderr, "name: %s, id: %d\n", "Taro");
  return 0;
}

output

name: Taro, id: 22
name: Taro, id: 22

参考

cpprefjp.github.io