Can __STDC_VERSION__ be trusted to guarantee full C standard support?
0 reputation · 29 Oct 2020, 13:47 UTC
0 reputation · 29 Oct 2020, 13:47 UTC
Determine whether a program can rely on the __STDC_VERSION__ macro to detect that a compiler fully implements a target C standard and safely fall back when an upgrade fails.
-std=) or a preceding #define can override the value, masking real capabilities.<stdatomic.h> or <threads.h> are not reflected by the macro.1. Does a matching __STDC_VERSION__ value guarantee that all language and library features of that standard are available?
2. What practical checks (e.g., __has_include, feature‑test macros) should supplement the macro to avoid false positives during a failed upgrade?
33775 reputation · 29 Oct 2020, 17:50 UTC
__STDC_VERSION__ macro cannot be trusted to guarantee full support of a C standard.The__STDC_VERSION__ macro expands to a long integer representing the year of the standard (e.g., 201112L for C11, 201710L for C17). However, relying on it alone for feature-detection is risky for several reasons:
<stdatomic.h> or <threads.h>.-std=c99 can force the compiler to report an older version value for a specific translation unit, regardless of the compiler's actual capabilities in newer standards.To ensure your code safely falls back when a compiler upgrade fails, you should supplement the version check with feature-test macros and probes:
__STDC_LIB_ATOMICS__ or __STDC_THREADS__ instead of just checking the global version number.__has_include("header.h") in C11 to verify that required headers exist before attempting to include them, preventing compilation errors on older environments.#if ____STDC_VERSION__ >= 201112L && defined(__STDC_LIB_ATOMICS__)
// Use C11 atomics
#else
// Fallback to older standard or custom implementation
#endif
Diagnostic request: Are you targeting a specific toolchain (e.g., embedded glibc vs. musl) where the library version lags significantly behind the compiler version?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.