diff options
| author | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
|---|---|---|
| committer | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
| commit | b3e9e62599532050fc776c5e8f076915b56c2235 (patch) | |
| tree | af252346106a61b18cc6fc6fdbd32e962d096f1c /test-lib.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 'test-lib.c')
| -rw-r--r-- | test-lib.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/test-lib.c b/test-lib.c new file mode 100644 index 0000000..9430df0 --- /dev/null +++ b/test-lib.c @@ -0,0 +1,46 @@ +#include "c23-fallback.h" +#include <stdio.h> + +typedef char const* key; + + +int comp_key(key const*a, key const*b) { + key A = *a; + key B = *b; + return strcmp(A, B); +} + +int comp(void const*a, void const*b) { + key const*A = a; + key const*B = b; + return comp_key(A, B); +} + +int main() { + static key data[] = { + "Tina", + "Albert", + "Xaver", + "Albertina", + }; + + qsort(data, (sizeof data)/(sizeof data[0]), sizeof data[0], comp); + for (size_t i = 0; i < (sizeof data)/(sizeof data[0]); ++i) + printf("%zu: \"%s\"\n", i, data[i]); + key k = "Tina"; + key* n1 = bsearch(&k, data, (sizeof data)/(sizeof data[0]), sizeof data[0], comp); + printf("found: \"%s\"\n", n1 ? *n1 : "unknown"); + key const* d = data; + key const* n2 = bsearch(&k, d, (sizeof data)/(sizeof data[0]), sizeof data[0], comp); + printf("found: \"%s\"\n", n2 ? *n2 : "unknown"); + char buf[25] = { 0 }; + strfroml(buf, 25, "%.10g", 3744.0083383L); + printf("strfroml %.10LG → \"%s\"\n", 3744.0083383L, buf); + strfroml(buf, 25, "%.10Lg", 3744.0083383L); + printf("invalid format leads to \"%s\"\n", buf); + for (char const* p = buf; p < buf+16; ++p) + printf("alignment %p: %zu, suitable for int*: %d\n", p, memalignment(p), memalignment(p)>=alignof(int)); + struct two {int a; int b; }; + assert((struct two){ .a = 0, .b = 1, }.b); + assert((struct two){ .a = 0, .b = 1, }.a); +} |
