1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語の可変長引数を取る関数サンプル

Last updated at Posted at 2019-01-06

C言語のstdargが難しいので、自分なりのサンプルを作成してみました。man stdargでstdargライブラリのマニュアルも合わせてご確認ください。

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

int sum(int num1, ...) {
    va_list list;
    int n;
    int sum = num1;
    if (sum == 0) return 0;
    va_start(list, num1);
    while (1) {
        n = va_arg(list, int);
        if (n == 0) break;
        sum += n;
    }
    va_end(list);
    return sum;
}


int main() {
    int res = sum(1,2,3,4,5,6,7,8,9,10,0);
    printf("sum = %d\n", res);
    return 0;
}
1
1
2

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?