From 3c379fbfb2b7301cd5c4f5371a9f0b96a1369b60 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 4 Mar 2022 13:18:35 +0100 Subject: Updated printf formatting to portable code. This was also to use http://winlibs.com gcc+clang with ucrt runtime-libs without warnings. --- examples/birthday.c | 4 ++-- examples/bits.c | 12 ++++++------ examples/books.c | 2 +- examples/box2.c | 12 ++++++------ examples/complex.c | 2 +- examples/csmap_erase.c | 4 ++-- examples/csmap_find.c | 2 +- examples/cstr_match.c | 2 +- examples/demos.c | 10 +++++----- examples/make.sh | 13 ++++++++----- examples/mapmap.c | 8 ++++---- examples/new_queue.c | 4 ++-- examples/prime.c | 6 +++--- examples/priority.c | 4 ++-- examples/queue.c | 2 +- examples/random.c | 6 +++--- examples/rawptr_elements.c | 2 +- examples/regex2.c | 2 +- examples/regex_match.c | 2 +- 19 files changed, 51 insertions(+), 48 deletions(-) (limited to 'examples') diff --git a/examples/birthday.c b/examples/birthday.c index f848436c..94438077 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -24,7 +24,7 @@ static void test_repeats(void) c_forrange (i, N) { uint64_t k = stc64_rand(&rng) & mask; int v = cmap_ic_insert(&m, k, 0).ref->second += 1; - if (v > 1) printf("repeated value %zu (%d) at 2^%d\n", k, v, (int) log2((double) i)); + if (v > 1) printf("repeated value %" PRIuMAX " (%d) at 2^%d\n", k, v, (int) log2((double) i)); } } } @@ -52,7 +52,7 @@ void test_distribution(void) sum /= map.size; c_foreach (i, cmap_x, map) { - printf("%4u: %zu - %zu: %11.8f\n", i.ref->first, i.ref->second, sum, (1 - (double) i.ref->second / sum)); + printf("%4u: %" PRIuMAX " - %" PRIuMAX ": %11.8f\n", i.ref->first, i.ref->second, sum, (1 - (double) i.ref->second / sum)); } } } diff --git a/examples/bits.c b/examples/bits.c index ac59bd32..9fdf1d5d 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -4,11 +4,11 @@ int main() { c_autovar (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { - printf("count %zu, %zu\n", cbits_count(set), set.size); + printf("count %" PRIuMAX ", %" PRIuMAX "\n", cbits_count(set), set.size); cbits s1 = cbits_new("1110100110111"); char buf[256]; cbits_to_str(s1, buf, 0, -1); - printf("buf: %s: %zu\n", buf, cbits_count(s1)); + printf("buf: %s: %" PRIuMAX "\n", buf, cbits_count(s1)); cbits_drop(&s1); cbits_reset(&set, 9); @@ -16,7 +16,7 @@ int main() c_autobuf (str, char, set.size + 1) printf(" str: %s\n", cbits_to_str(set, str, 0, -1)); - printf("%4zu: ", set.size); + printf("%4" PRIuMAX ": ", set.size); c_forrange (i, int, set.size) printf("%d", cbits_test(set, i)); puts(""); @@ -26,12 +26,12 @@ int main() cbits_resize(&set, 93, false); cbits_resize(&set, 102, true); cbits_set_value(&set, 99, false); - printf("%4zu: ", set.size); + printf("%4" PRIuMAX ": ", set.size); c_forrange (i, int, set.size) printf("%d", cbits_test(set, i)); puts("\nIterate:"); - printf("%4zu: ", set.size); + printf("%4" PRIuMAX ": ", set.size); c_forrange (i, int, set.size) printf("%d", cbits_test(set, i)); puts(""); @@ -53,7 +53,7 @@ int main() puts(""); cbits_set_all(&set, false); - printf("%4zu: ", set.size); + printf("%4" PRIuMAX ": ", set.size); c_forrange (i, int, set.size) printf("%d", cbits_test(set, i)); puts(""); diff --git a/examples/books.c b/examples/books.c index 8ddce634..10b5c416 100644 --- a/examples/books.c +++ b/examples/books.c @@ -32,7 +32,7 @@ int main() // When collections store owned values (String), they can still be // queried using references (&str). if (cmap_str_contains(&book_reviews, "Les Misérables")) { - printf("We've got %zu reviews, but Les Misérables ain't one.", + printf("We've got %" PRIuMAX " reviews, but Les Misérables ain't one.", cmap_str_size(book_reviews)); } diff --git a/examples/box2.c b/examples/box2.c index d5905877..e5b4dacd 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -67,22 +67,22 @@ int main(void) { // Double indirection box_in_a_box = cbox_BoxPoint_from(boxed_origin()); - printf("Point occupies %zu bytes on the stack\n", + printf("Point occupies %" PRIuMAX " bytes on the stack\n", sizeof(point)); - printf("Rectangle occupies %zu bytes on the stack\n", + printf("Rectangle occupies %" PRIuMAX " bytes on the stack\n", sizeof(rectangle)); // box size == pointer size - printf("Boxed point occupies %zu bytes on the stack\n", + printf("Boxed point occupies %" PRIuMAX " bytes on the stack\n", sizeof(boxed_point)); - printf("Boxed rectangle occupies %zu bytes on the stack\n", + printf("Boxed rectangle occupies %" PRIuMAX " bytes on the stack\n", sizeof(boxed_rectangle)); - printf("Boxed box occupies %zu bytes on the stack\n", + printf("Boxed box occupies %" PRIuMAX " bytes on the stack\n", sizeof(box_in_a_box)); // Copy the data contained in `boxed_point` into `unboxed_point` Point unboxed_point = *boxed_point.get; - printf("Unboxed point occupies %zu bytes on the stack\n", + printf("Unboxed point occupies %" PRIuMAX " bytes on the stack\n", sizeof(unboxed_point)); } } diff --git a/examples/complex.c b/examples/complex.c index b0cd7a01..9ccd2dae 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -41,7 +41,7 @@ int main() { // Put in some data in stack array stack.data[x] = 3.1415927f; - printf("stack size: %zu\n", FloatStack_size(stack)); + printf("stack size: %" PRIuMAX "\n", FloatStack_size(stack)); StackList list = StackList_init(); StackList_push_back(&list, stack); diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 68a0378b..2716ad8f 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -12,7 +12,7 @@ void printmap(mymap m) { c_foreach (elem, mymap, m) printf(" [%d, %s]", elem.ref->first, elem.ref->second.str); - printf("\nsize() == %zu\n\n", mymap_size(m)); + printf("\nsize() == %" PRIuMAX "\n\n", mymap_size(m)); } int main() @@ -72,7 +72,7 @@ int main() // The 3rd member function removes elements with a given Key size_t count = mymap_erase(&m3, 2); // The 3rd member function also returns the number of elements removed - printf("The number of elements removed from m3 is: %zu\n", count); + printf("The number of elements removed from m3 is: %" PRIuMAX "\n", count); puts("After the element with a key of 2 is deleted, the map m3 is:"); printmap(m3); } diff --git a/examples/csmap_find.c b/examples/csmap_find.c index ea86d71b..9e3aba83 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -18,7 +18,7 @@ void print_elem(csmap_istr_raw p) { #define using_print_collection(CX) \ void print_collection_##CX(CX t) { \ - printf("%zu elements: ", CX##_size(t)); \ + printf("%" PRIuMAX " elements: ", CX##_size(t)); \ \ c_foreach (p, CX, t) { \ print_elem(CX##_value_toraw(p.ref)); \ diff --git a/examples/cstr_match.c b/examples/cstr_match.c index db5b3ecf..057334f6 100644 --- a/examples/cstr_match.c +++ b/examples/cstr_match.c @@ -5,7 +5,7 @@ int main() { c_autovar (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { size_t pos = cstr_find_n(ss, "brown", 0, 5); - printf("%zu [%s]\n", pos, pos == cstr_npos ? "" : &ss.str[pos]); + printf("%" PRIuMAX " [%s]\n", pos, pos == cstr_npos ? "" : &ss.str[pos]); printf("equals: %d\n", cstr_equals(ss, "The quick brown fox jumps over the lazy dog.JPG")); printf("contains: %d\n", cstr_contains(ss, "umps ove")); printf("starts_with: %d\n", cstr_starts_with(ss, "The quick brown")); diff --git a/examples/demos.c b/examples/demos.c index 93192ee5..e91be28a 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -41,14 +41,14 @@ void vectordemo1() for (size_t i = 10; i <= 100; i += 10) cvec_ix_push_back(&bignums, i * i); - printf("erase - %d: %zu\n", 3, bignums.data[3]); + printf("erase - %d: %" PRIuMAX "\n", 3, bignums.data[3]); cvec_ix_erase_n(&bignums, 3, 1); // erase index 3 cvec_ix_pop_back(&bignums); // erase the last cvec_ix_erase_n(&bignums, 0, 1); // erase the first for (size_t i = 0; i < cvec_ix_size(bignums); ++i) { - printf("%zu: %zu\n", i, bignums.data[i]); + printf("%" PRIuMAX ": %" PRIuMAX "\n", i, bignums.data[i]); } } } @@ -174,11 +174,11 @@ void mapdemo3() cmap_str_iter it = cmap_str_find(&table, "Make"); c_foreach (i, cmap_str, table) printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str); - printf("size %zu: remove: Make: %s\n", cmap_str_size(table), it.ref->second.str); + printf("size %" PRIuMAX ": remove: Make: %s\n", cmap_str_size(table), it.ref->second.str); //cmap_str_erase(&table, "Make"); cmap_str_erase_at(&table, it); - printf("size %zu\n", cmap_str_size(table)); + printf("size %" PRIuMAX "\n", cmap_str_size(table)); c_foreach (i, cmap_str, table) printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str); cmap_str_drop(&table); // frees key and value cstrs, and hash table. @@ -198,7 +198,7 @@ void arraydemo1() float **arr2 = arr3.data[5]; float *arr1 = arr3.data[5][4]; - printf("arr3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(arr3), + printf("arr3: %" PRIuMAX ": (%" PRIuMAX ", %" PRIuMAX ", %" PRIuMAX ") = %" PRIuMAX "\n", sizeof(arr3), arr3.xdim, arr3.ydim, arr3.zdim, carr3_f_size(arr3)); printf("%g\n", arr1[3]); // = 10.2 diff --git a/examples/make.sh b/examples/make.sh index ccce036f..3f2247b6 100644 --- a/examples/make.sh +++ b/examples/make.sh @@ -1,11 +1,14 @@ #!/bin/bash -cc='gcc -O2 -Wall -std=c99 -pedantic' +cc='gcc -s -O2 -Wall -std=c99 -pedantic' +#cc='clang -s -O2 -Wall -std=c99 -pedantic' #cc='clang' #cc='clang -c -DSTC_HEADER' -#cc='cl -nologo' +#cc='cl -O2 -nologo -W2 -MD' #cc='cl -nologo -TP' #cc='cl -nologo -std:c11' libs='' +#oflag='/Fe:' +oflag='-o ' run=0 if [ -z "$OS" ]; then libs='-lm -pthread' @@ -24,8 +27,8 @@ fi if [ $run = 0 ] ; then for i in *.c ; do - echo $cc -I../include $i $libs - $cc -I../include $i $libs + echo $cc -I../include $i $libs $oflag$(basename $i .c).exe + $cc -I../include $i $libs $oflag$(basename $i .c).exe done else for i in *.c ; do @@ -37,4 +40,4 @@ else done fi -rm -f a.out *.o *.obj *.exe +rm -f a.out *.o *.obj # *.exe diff --git a/examples/mapmap.c b/examples/mapmap.c index f0bf31a8..b3360d1e 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -58,10 +58,10 @@ int main(void) printf("%s: %s - %s\n", i.ref->first.str, _.name.str, _.email.str); puts(""); - c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Nick Denton"))); - c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Patrick Dust"))); - c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Dennis Kay"))); - c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Serena Bath"))); + c_auto (Stack, s) printf("found: %" PRIuMAX "\n", Stack_size(s = contains(&map, "Nick Denton"))); + c_auto (Stack, s) printf("found: %" PRIuMAX "\n", Stack_size(s = contains(&map, "Patrick Dust"))); + c_auto (Stack, s) printf("found: %" PRIuMAX "\n", Stack_size(s = contains(&map, "Dennis Kay"))); + c_auto (Stack, s) printf("found: %" PRIuMAX "\n", Stack_size(s = contains(&map, "Serena Bath"))); puts("Done"); } } diff --git a/examples/new_queue.c b/examples/new_queue.c index 718c025f..1b7c021c 100644 --- a/examples/new_queue.c +++ b/examples/new_queue.c @@ -31,7 +31,7 @@ int main() { cqueue_int_push(&Q, stc64_uniform(&rng, &dist)); // Push or pop on the queue ten million times - printf("befor: size %zu, capacity %zu\n", cqueue_int_size(Q), cqueue_int_capacity(Q)); + printf("befor: size %" PRIuMAX ", capacity %" PRIuMAX "\n", cqueue_int_size(Q), cqueue_int_capacity(Q)); for (int i=n; i>0; --i) { int r = stc64_uniform(&rng, &dist); if (r & 1) @@ -39,6 +39,6 @@ int main() { else cqueue_int_pop(&Q); } - printf("after: size %zu, capacity %zu\n", cqueue_int_size(Q), cqueue_int_capacity(Q)); + printf("after: size %" PRIuMAX ", capacity %" PRIuMAX "\n", cqueue_int_size(Q), cqueue_int_capacity(Q)); } } diff --git a/examples/prime.c b/examples/prime.c index 0e2ede56..4d70ee24 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -24,7 +24,7 @@ cbits sieveOfEratosthenes(size_t n) int main(void) { size_t n = 1000000000; - printf("computing prime numbers up to %zu\n", n); + printf("computing prime numbers up to %" PRIuMAX "\n", n); clock_t t1 = clock(); c_autovar (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) { @@ -32,10 +32,10 @@ int main(void) size_t np = cbits_count(primes); clock_t t2 = clock(); - printf("number of primes: %zu, time: %f\n", np, (t2 - t1) / (float)CLOCKS_PER_SEC); + printf("number of primes: %" PRIuMAX ", time: %f\n", np, (t2 - t1) / (float)CLOCKS_PER_SEC); printf("2"); for (size_t i = 3; i < 1000; i += 2) - if (cbits_test(primes, i>>1)) printf(" %zu", i); + if (cbits_test(primes, i>>1)) printf(" %" PRIuMAX "", i); puts(""); } } diff --git a/examples/priority.c b/examples/priority.c index 184cad9f..1767dba8 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -15,7 +15,7 @@ int main() { c_auto (cpque_i, heap) { // Push ten million random numbers to priority queue - printf("Push %zu numbers\n", N); + printf("Push %" PRIuMAX " numbers\n", N); c_forrange (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); @@ -27,7 +27,7 @@ int main() { puts("Extract the hundred smallest."); c_forrange (100) { - printf("%zd ", *cpque_i_top(&heap)); + printf("%" PRIdMAX " ", *cpque_i_top(&heap)); cpque_i_pop(&heap); } } diff --git a/examples/queue.c b/examples/queue.c index 2c2052e5..db5f0e05 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -26,6 +26,6 @@ int main() { else --n, cqueue_i_pop(&queue); } - printf("%d, %zu\n", n, cqueue_i_size(queue)); + printf("%d, %" PRIuMAX "\n", n, cqueue_i_size(queue)); } } diff --git a/examples/random.c b/examples/random.c index 81fe71c5..59c1eb95 100644 --- a/examples/random.c +++ b/examples/random.c @@ -18,7 +18,7 @@ int main() sum += (uint32_t) stc64_rand(&rng); } diff = clock() - before; - printf("full range\t\t: %f secs, %zu, avg: %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("full range\t\t: %f secs, %" PRIuMAX ", avg: %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); stc64_uniform_t dist1 = stc64_uniform_init(0, range); rng = stc64_init(seed); @@ -28,7 +28,7 @@ int main() sum += stc64_uniform(&rng, &dist1); // unbiased } diff = clock() - before; - printf("unbiased 0-%zu\t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("unbiased 0-%" PRIuMAX "\t: %f secs, %" PRIuMAX ", avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); sum = 0; rng = stc64_init(seed); @@ -37,6 +37,6 @@ int main() sum += stc64_rand(&rng) % (range + 1); // biased } diff = clock() - before; - printf("biased 0-%zu \t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("biased 0-%" PRIuMAX " \t: %f secs, %" PRIuMAX ", avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); } diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c index 3ff0dc8a..483e7c9a 100644 --- a/examples/rawptr_elements.c +++ b/examples/rawptr_elements.c @@ -57,6 +57,6 @@ int main() cmap_str_emplace(&map, "goodbye", 400); c_forpair (name, number, cmap_str, map) - printf("%s: %zd\n", _.name.str, *_.number); + printf("%s: %" PRIdMAX "\n", _.name.str, *_.number); } } diff --git a/examples/regex2.c b/examples/regex2.c index 96ac7ed4..d27b0406 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -25,7 +25,7 @@ int main() c_forrange (j, cregex_captures(re)) { csview cap = {m[j].str, m[j].len}; - printf(" submatch %zu: " c_PRIsv "\n", j, c_ARGsv(cap)); + printf(" submatch %" PRIuMAX ": " c_PRIsv "\n", j, c_ARGsv(cap)); } puts(""); } diff --git a/examples/regex_match.c b/examples/regex_match.c index f543fc31..cc5bb483 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -20,7 +20,7 @@ int main() printf("%d\n", res); cregmatch m[10]; if (cregex_find(&re, s, 10, m, 0) > 0) { - printf("Found digits at position %zu-%zu\n", m[0].str - s, m[0].str - s + m[0].len); + printf("Found digits at position %" PRIuMAX "-%" PRIuMAX "\n", m[0].str - s, m[0].str - s + m[0].len); } else { printf("Could not find any digits\n"); } -- cgit v1.2.3