← Back to davo.co
aboutsummaryrefslogtreecommitdiffstats
path: root/va_arg.c
diff options
context:
space:
mode:
authorDavid Faulkner <[email protected]>2026-08-07 23:40:47 -0500
committerDavid Faulkner <[email protected]>2026-08-07 23:40:47 -0500
commitb3e9e62599532050fc776c5e8f076915b56c2235 (patch)
treeaf252346106a61b18cc6fc6fdbd32e962d096f1c /va_arg.c
Import official C23 code examples for Modern C (Jens Gustedt, 2024)HEADupstream-importmain
- Add official C source files, Makefile, c23-fallback.h, and LICENSE - Update README.md with study mirror notice
Diffstat (limited to 'va_arg.c')
-rw-r--r--va_arg.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/va_arg.c b/va_arg.c
new file mode 100644
index 0000000..1b2c0a8
--- /dev/null
+++ b/va_arg.c
@@ -0,0 +1,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;
+}