From b3e9e62599532050fc776c5e8f076915b56c2235 Mon Sep 17 00:00:00 2001 From: David Faulkner Date: Fri, 7 Aug 2026 23:40:47 -0500 Subject: Import official C23 code examples for Modern C (Jens Gustedt, 2024) - Add official C source files, Makefile, c23-fallback.h, and LICENSE - Update README.md with study mirror notice --- va_arg.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 va_arg.c (limited to 'va_arg.c') 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 +#include +//#include + +/** + ** @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; +} -- cgit v1.2.3