首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> C 語言問答

C語言必學標準庫函式:從 printf/scanf 到完整函式表

     編輯:C 語言問答

新手最先必會的兩個函式

#include <stdio.h>
int main(void) {
    int x;
    printf("請輸入整數: ");
    if (scanf("%d", &x) == 1) {
        printf("你輸入了 %d\n", x);
    }
    return 0;
}
  • `printf`:格式化輸出到 `stdout`。回傳輸出字元數,失敗回負值。
  • `scanf`:格式化從 `stdin` 讀取。**必須傳址 (`&x`)**,回傳成功賦值的項目數 (EOF 表示輸入結束/錯誤)。

**陷阱**:`scanf("%d", &x)` 留下換行符在緩衝區,後續 `%c`/`%s`/`fgets` 會讀到空行。解法:`scanf(" %c", &c)` (前置空白跳過空白字元) 或改用 `fgets` + `sscanf`/`strtol`。

C 標準庫 29 個標頭檔完整分類表 (C11)

標頭檔主要功能常用函式/巨集/型別
<assert.h>執行期斷言`assert(expr)`
<complex.h>複數運算 (C99)`complex`, `I`, `cabs`, `csin`, `cpow`
<ctype.h>字元分類/轉換`isdigit`, `isalpha`, `tolower`, `toupper`
<errno.h>錯誤碼`errno`, `EDOM`, `ERANGE`, `EILSEQ`
<fenv.h>浮點環境 (C99)`feclearexcept`, `fegetround`, `feraiseexcept`
<float.h>浮點型別極限`FLT_MAX`, `DBL_EPSILON`, `DECIMAL_DIG`
<inttypes.h>定寬整數格式 (C99)`PRId64`, `SCNd32`, `intmax_t`, `uintptr_t`
<iso646.h>替代運算子拼寫`and`, `or`, `not`, `xor`, `bitand`, `bitor`
<limits.h>整數型別極限`INT_MAX`, `LONG_MIN`, `CHAR_BIT`
<locale.h>地區設定`setlocale`, `localeconv`, `lconv`
<math.h>數學函式`sin`, `cos`, `sqrt`, `log`, `exp`, `hypot`, `fma`
<setjmp.h>非局部跳轉`setjmp`, `longjmp`, `jmp_buf`
<signal.h>訊號處理`signal`, `raise`, `SIGINT`, `SIGSEGV`, `sig_atomic_t`
<stdalign.h>對齊關鍵字 (C11)`alignas`, `alignof`, `max_align_t`
<stdarg.h>可變參數`va_list`, `va_start`, `va_arg`, `va_end`, `va_copy`
<stdatomic.h>原子操作 (C11)`atomic_int`, `atomic_load`, `atomic_store`, `atomic_compare_exchange_strong`
<stdbool.h>布林型別 (C99)`bool`, `true`, `false`, `__bool_true_false_are_defined`
<stddef.h>通用定義`size_t`, `ptrdiff_t`, `NULL`, `offsetof`, `max_align_t`
<stdint.h>定寬整數型別 (C99)`int8_t`, `uint32_t`, `intptr_t`, `SIZE_MAX`
<stdio.h>輸入輸出`printf`, `scanf`, `fopen`, `fclose`, `fread`, `fwrite`, `fgets`, `fprintf`, `snprintf`, `FILE`, `stdin`, `stdout`, `stderr`
<stdlib.h>通用工具`malloc`, `free`, `calloc`, `realloc`, `exit`, `abort`, `atexit`, `rand`, `srand`, `strtol`, `strtod`, `abs`, `labs`, `llabs`, `div`, `ldiv`, `bsearch`, `qsort`, `getenv`, `system`
<stdnoreturn.h>不返回函式標記 (C11)`noreturn`
<string.h>字串/記憶體操作`strcpy`, `strncpy`, `strcat`, `strncat`, `strcmp`, `strncmp`, `strlen`, `strchr`, `strstr`, `memcpy`, `memmove`, `memset`, `memcmp`, `memchr`
<tgmath.h>型別泛型數學 (C99)`sin`, `cos`, `sqrt` 等自動根據引數型別選擇 `float`/`double`/`long double`/`complex` 版本
<threads.h>執行緒 (C11, 選擇性)`thrd_create`, `thrd_join`, `mtx_init`, `mtx_lock`, `cnd_wait`, `thread_local`
<time.h>日期時間`time`, `clock`, `difftime`, `strftime`, `gmtime`, `localtime`, `struct tm`, `timespec`, `CLOCKS_PER_SEC`
<uchar.h>Unicode 字元 (C11)`char16_t`, `char32_t`, `c16rtomb`, `mbrtoc16`, `mbstate_t`
<wchar.h>寬字元`wchar_t`, `wprintf`, `wcscpy`, `wcslen`, `mbrtowc`, `wcrtomb`
<wctype.h>寬字元分類`iswalpha`, `iswdigit`, `towlower`, `wctrans`

高頻必會核心集 (背熟這 30 個覆蓋 90% 日常開發)

  1. I/O: `printf`, `fprintf`, `snprintf`, `fopen`, `fclose`, `fread`, `fwrite`, `fgets`, `feof`, `ferror`, `perror`
  2. 記憶體: `malloc`, `calloc`, `realloc`, `free`, `memcpy`, `memmove`, `memset`, `memcmp`
  3. 字串: `strlen`, `strcmp`, `strncmp`, `strcpy`, `strncpy`, `strcat`, `strncat`, `strchr`, `strrchr`, `strstr`, `strtol`, `strtod`, `strerror`
  4. 工具: `exit`, `atexit`, `qsort`, `bsearch`, `abs`, `labs`, `llabs`, `rand`, `srand`, `getenv`
  5. 時間: `time`, `clock`, `strftime`, `gmtime`, `localtime`, `difftime`
  6. 錯誤/斷言: `errno`, `assert`, `setjmp`/`longjmp` (慎用)
  7. 型別安全: `size_t`, `ptrdiff_t`, `intptr_t`, `uintptr_t`, `int64_t`, `uint32_t`, `INT_MAX`, `SIZE_MAX`

學習建議

  • **不要死背**。遇到需求 → 查標頭檔 → 閱讀 man page (`man 3 printf`) → 寫小測試 → 納入個人片段庫。
  • **警惕非標準函式**:`gets` (已移除)、`strlcpy` (BSD)、`itoa` (Windows)、`conio.h` 全家 — **不具可移植性**。
  • **善用 `_Generic` (C11) 封裝型別泛型介面** — 如 `MAX(x,y)` 自動適配 `int`/`double`/`long long`。

總結

C 標準庫雖小,卻是**所有 C 程式的基石**。熟練掌握上述高頻集,配合 `man` 手冊查閱細節,即可應付絕大多數工程場景。記住:`stdio.h`、`stdlib.h`、`string.h`、`stdint.h`、`stddef.h` 是「五大金剛」,隨身攜帶。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved