for (int i = 0; i <= 100; i++) { ... } // 報錯:'for' loop initial declarations are only allowed in C99 mode
| C 標準 | for 循環初始化聲明 | 主流編譯器預設行為 |
|---|---|---|
| C89/C90 | 不允許 | GCC (預設 -std=gnu90)、MSVC (預設 C89 兼容) 均報錯 |
| C99 | 允許 | GCC -std=c99、Clang 預設支援 |
| C11/C18 | 允許 | 同 C99 |
| C++98 起 | 允許 | MSVC /TP、G++ 預設支援 |
int i;
for (i = 0; i <= 100; i++) { ... }
適用所有 C 標準、所有編譯器,推薦嵌入式/跨平台專案。-std=c99 或 -std=gnu99.c 改為 .cpp 或強制 C++ 編譯(/TP),C++ 從第一版標準就允許。/TP),同時獲得 RAII、模板等現代特性。// 單行註釋long long、_Bool、_Complexint arr[n];inline 關鍵字、restrict 限定符struct S s = {.field = 1};理解標準差異,根據專案工具鏈選擇合適方案,避免「在我機器上能跑」的移植陷阱。