/* * sample-c23.c: features new in C23 (ISO/IEC 9899:2024). * Free sample from 99helpers.com/file-types/c * * Needs a compiler in C23 mode. It fails under -std=c17, which makes it a * handy test of which standard a build uses. * gcc -Wall -Wextra -std=c23 sample-c23.c -o sample-c23 (GCC 14 and later; * GCC 13 calls it -std=c2x; GCC 15 and * later use C23 by default) * clang -Wall -Wextra -std=c23 sample-c23.c -o sample-c23 (Clang 18 and later) */ #include /* [[attributes]] use the same syntax as C++. nodiscard warns if a caller * ignores the result. */ [[nodiscard]] static int clamp_percent(int value) { return value < 0 ? 0 : value > 100 ? 100 : value; } int main(void) { /* bool, true and false are keywords now: no needed. */ bool ready = true; /* nullptr is a real null pointer constant with its own type. */ const char *label = nullptr; /* Binary literals and digit separators make constants readable. */ unsigned flags = 0b1010'0101; long long population = 8'100'000'000; /* typeof, long a GCC extension, is now standard: it names the type of * an expression, here long long. */ typeof(population) copy = population / 1'000; /* static_assert no longer needs a message, and needs no header. */ static_assert(sizeof(long) >= 4); if (label == nullptr) { label = "C23"; } printf("%s: ready=%d flags=0x%X copy=%lld percent=%d\n", label, ready, flags, copy, clamp_percent(140)); printf("__STDC_VERSION__ = %ldL\n", (long)__STDC_VERSION__); return 0; }