不,C23 不完全向后兼容。并非所有 C17 程序都可以编译为 C23 程序。
除其他重大变化外,C23 还引入了新的关键字和声明说明符。使用这些标识符的 C17 程序不能解释为 C23 程序。
例如,以下所有内容在 C17 程序中有效,但在 C23 程序中无效。
int* nullptr = 0;
int true = 1;
int bool = 0;
int constexpr = 1;
void static_assert() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
更危险的是,以下代码片段在 C17 和 C23 中具有不同的含义
auto d = 1.5; // C23: `d` has type `double`
// C17: `d` has type `int`
void foo(); // C23: equivalent to `void foo(void);`
// C17: non-prototype function declaration
Run Code Online (Sandbox Code Playgroud)
有关 C23 主要变更的更完整说明,请参阅N3096 的附件 M.2(C23 最新工作草案,2023-04-01)。