LoginSignup
13
14

More than 5 years have passed since last update.

C言語で可変長引数をそのまま別の関数に渡したい

Posted at

単純に渡せそうにないです...

#include <stdio.h>
#include <stdarg.h>

/*
 * printfした後改行する
 */
void printfl(const char *fmt, ...)
{
  va_list list;
  va_start(list, fmt);
  vprintf(fmt, list);
  printf("\n");
  va_end(list);
}

void main()
{
  printfl("test:%d", 12345); // 12345
}

C言語で可変長引数を使う場合は stdarg.h をインクルードしておく必要があります。
また、渡す先でva_listに対応している必要があります。
printf に va_list を渡しても正しく動作しなかったので
vprintfを使っています。定義は以下の通り

int vprintf(const char *format, va_list ap);
13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14