#include <stdio.h>
int main(void) {
int x;
printf("請輸入整數: ");
if (scanf("%d", &x) == 1) {
printf("你輸入了 %d\n", x);
}
return 0;
}
**陷阱**:`scanf("%d", &x)` 留下換行符在緩衝區,後續 `%c`/`%s`/`fgets` 會讀到空行。解法:`scanf(" %c", &c)` (前置空白跳過空白字元) 或改用 `fgets` + `sscanf`/`strtol`。
| 標頭檔 | 主要功能 | 常用函式/巨集/型別 |
|---|---|---|
<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` |
C 標準庫雖小,卻是**所有 C 程式的基石**。熟練掌握上述高頻集,配合 `man` 手冊查閱細節,即可應付絕大多數工程場景。記住:`stdio.h`、`stdlib.h`、`string.h`、`stdint.h`、`stddef.h` 是「五大金剛」,隨身攜帶。