#include <stdio.h> // 僅包含 stdio.h
int main() {
char ch;
while ((ch = getchar()) != '\n') {
if (islower(ch)) n++; // 未 #include
else if (isupper(ch)) m++;
}
}
編譯通過、執行正常,未報警告 (視編譯器嚴格程度)。
islower, isupper, strlen, memcpy 等),直接產出優化機器碼或內建宣告,不需頭檔即可編譯。int、參數經預設提升。標準庫函式恰巧符合此簽名,故能「碰巧」通過連結。libc,符號解析成功。<ctype.h>?| 風險 | 說明 |
|---|---|
| 跨平台移植失敗 | MSVC、嵌入式編譯器可能無內建,直接報錯「未定義識別符」 |
| 巨集 vs 函式差異 | islower 可能是巨集,展開邏輯依賴頭檔定義;內建函式可能行為不一致 |
| 靜態分析誤報 | Cppcheck、Clang-Tidy、SonarQube 會標記「隱式宣告」警告,影響代碼品質門檻 |
| C99/C11 標準合規 | 隱式宣告已在 C99 移除,現代標準模式 (-std=c11 -pedantic) 直接報錯 |
// GCC/Clang
gcc test.c -std=c11 -pedantic // 警告:implicit declaration of function 'islower'
gcc test.c // 靜默通過 (預設 GNU 延伸)
// MSVC
cl test.c // 錯誤 C3861: 'islower': identifier not found
#include <ctype.h> 只佔幾十位元組編譯時間,換取 100% 可移植性。-Wall -Wextra -Wpedantic -Werror=implicit-function-declaration (GCC/Clang) / /W4 /WX (MSVC)。「能跑」不代表「正確」。養成 用到什麼函式,就包什麼頭檔 的肌肉記憶,是專業 C 程式設計師的基本素養。