Header file (.h) chứa khai báo hàm, macro, kiểu dữ liệu từ thư viện chuẩn. Include bằng #include <header.h>.
Bảng header chuẩn C quan trọng
| Header | Mô tả | Chính |
|---|---|---|
<stdio.h> | Input/Output chuẩn | printf, scanf, fopen, fclose, fgets, fprintf |
<stdlib.h> | Tiện ích chung | malloc, free, exit, atoi, strtol, rand, qsort, abs |
<string.h> | Thao tác chuỗi/byte | strcpy, strlen, strcmp, strcat, memset, memcpy |
<math.h> | Hàm toán học | sin, cos, sqrt, pow, log, exp, floor, ceil |
<ctype.h> | Phân loại ký tự | isalpha, isdigit, isspace, toupper, tolower |
<time.h> | Ngày giờ | time, clock, strftime, localtime, gmtime |
<stdint.h> | Kiểu int có độ rộng cố định (C99) | int32_t, uint64_t, intptr_t, SIZE_MAX |
<stdbool.h> | Kiểu boolean (C99) | bool, true, false |
<stddef.h> | Kiểu chung | size_t, ptrdiff_t, NULL, offsetof |
<limits.h> | Giới hạn kiểu số | INT_MAX, CHAR_BIT, LONG_MIN |
<float.h> | Giới hạn float | FLT_MAX, DBL_EPSILON, DECIMAL_DIG |
<errno.h> | Mã lỗi | errno, EDOM, ERANGE, EILSEQ |
<assert.h> | Assertion debug | assert() |
<setjmp.h> | Non-local jump | setjmp, longjmp |
<signal.h> | Xử lý signal | signal, raise, SIGINT, SIGSEGV |
<wchar.h> | Wide character | wchar_t, wprintf, wcscpy, wcslen |
<wctype.h> | Phân loại wide char | iswalpha, iswdigit, towupper |
<complex.h> | Số phức (C99) | complex, creal, cimag, cabs |
<tgmath.h> | Type-generic math (C99) | Macro tự chọn float/double/complex |
<inttypes.h> | Format specifier cho stdint | PRId32, PRIu64, SCNx64 |
| C Header | C++ Header | Namespace |
|---|---|---|
<stdio.h> | <cstdio> | std:: |
<stdlib.h> | <cstdlib> | std:: |
<string.h> | <cstring> | std:: |
<math.h> | <cmath> | std:: |
<time.h> | <ctime> | std:: |
C++ cũng có header riêng: <iostream>, <vector>, <string>, <memory>, <algorithm>, <chrono>...
Best Practice
- C:
#include <header.h> - C++:
#include <cheader>(trong namespace std) hoặc header C++ native - Luôn include header cần thiết (không phụ thuộc transitive include)
- Sắp xếp: system header trước, project header sau