編譯器在遇到未聲明就使用的標準庫函數(如 memset、printf、exit)時,會假定其返回 int 並採用默認參數提升規則,隨後發現與內建函數原型不匹配,從而發出警告:
warning: incompatible implicit declaration of built-in function 'memset'
| 函數 | 缺失頭文件 | 補充指令 |
|---|---|---|
memset、memcpy、strlen 等字符串/內存操作 | <string.h> | #include <string.h> |
printf、scanf、sprintf 等 I/O | <stdio.h> | #include <stdio.h> |
malloc、free、exit、atoi 等 | <stdlib.h> | #include <stdlib.h> |
sin、cos、sqrt 等數學 | <math.h> | #include <math.h> 並鏈接 -lm |
-std=c99 -pedantic-errors 可升級為錯誤,強制規範代碼。-Wall -Wextra -Werror=implicit-function-declaration。-std=c11 或 -std=c17,配合 -pedantic。「缺頭文件」是新手最常見、也是最易修復的警告。養成用什麼庫函數,必包對應頭文件的肌肉記憶,代碼更健壯、可移植性更強。