← Back to davo.co
aboutsummaryrefslogtreecommitdiffstats
path: root/fibonacci2.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 /fibonacci2.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 'fibonacci2.c')
-rw-r--r--fibonacci2.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/fibonacci2.c b/fibonacci2.c
new file mode 100644
index 0000000..84d3582
--- /dev/null
+++ b/fibonacci2.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include "c23-fallback.h"
+
+void fib2rec(size_t n, size_t buf[static 2]) [[__unsequenced__]] {
+ if (n > 2) {
+ size_t res = buf[0] + buf[1];
+ buf[1] = buf[0];
+ buf[0] = res;
+ fib2rec(n-1, buf);
+ }
+}
+
+size_t fib2(size_t n) [[__unsequenced__]] {
+ size_t res[2] = { 1, 1, };
+ fib2rec(n, res);
+ return res[0];
+}
+
+int main(int argc, char* argv[argc+1]) {
+ for (int i = 1; i < argc; ++i) { // Processes args
+ size_t const n = strtoull(argv[i], nullptr, 0); // arg -> size_t
+ printf("fib(%zu) is %zu\n",
+ n, fib2(n));
+ }
+ return EXIT_SUCCESS;
+}