← Back to davo.co
summaryrefslogtreecommitdiffstats
path: root/va_arg.c
blob: 1b2c0a8bcca4592b1baaee6b19184040bb1c6193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "c23-fallback.h"
#include <stdarg.h>
#include <stdio.h>
//#include <stdlib.h>

/**
 ** @brief A small, useless function to show how variadic
 ** functions work
 **/
double sumIt(size_t n, ...) {
  double ret = 0.0;
  va_list va;
  va_start(va);
  for (size_t i = 0; i < n; ++i)
    ret += va_arg(va, double);
  va_end(va);
  return ret;
}

/**
 ** @brief A simple debug stream
 **
 ** Per convention no output is produced when this is null.
 **
 ** This can be set local to the current thread, such that threads may
 ** print their debug messages to different files or streams.
 **/
thread_local FILE* iodebug = nullptr;

/**
 ** @brief Prints to the debug stream @c iodebug
 **/
[[gnu::format(printf, 1, 2)]]
int printf_debug(const char *format, ...) {
  int ret = 0;
  if (iodebug) {
    va_list va;
    va_start(va);
    ret = vfprintf(iodebug, format, va);
    va_end(va);
  }
  return ret;
}



int main(int argc, char* argv[argc+1]) {
  if (argc < 4) return EXIT_FAILURE;
  iodebug = stderr;
  printf_debug("%g\n", sumIt(3, strtod(argv[1], nullptr), strtod(argv[2], nullptr), strtod(argv[3], nullptr)));
  return EXIT_SUCCESS;
}