为何GCC 9.3.0仍未实现gets_s()?开启-std=c11编译选项仍无效
gets_s() implemented in GCC 9.3.0 even with -std=c11? Great question—this boils down to how C standards handle optional features and the design priorities of the GNU C Library (glibc), which GCC depends on for standard library functionality. Let’s break it down clearly:
gets_s()belongs to an optional C11 annex
Thegets_s()function is part of C11's Annex K, which defines "Bounds-checking interfaces". Unlike core standard functions that every conforming implementation must support, Annex K functions are explicitly marked as optional. The C standard doesn’t mandate compilers or libraries to include these functions, so many implementations (including glibc) choose not to.Glibc’s position on Annex K
The glibc maintainers have publicly criticized Annex K’s design, citing issues like inflexible interfaces, unnecessary compatibility breaks with existing code, and the fact that widely adopted safer alternatives (likefgets(), which you mentioned) already solve the problem of unsafe input. For these reasons, glibc has never implemented any Annex K functions—includinggets_s()—even in versions that fully support C11.Practical alternatives for your code
If you need bounds-checked string input, stick withfgets(): it’s portable across all standard-compliant C implementations and eliminates the critical security flaws of the oldgets()function (which is now completely removed from the C standard). If you specifically require behavior matchinggets_s(), you’d need to use a compiler/library that implements Annex K (like Microsoft Visual C++) or build a simple wrapper aroundfgets()to replicate its behavior.
内容的提问来源于stack exchange,提问作者brushmonk

