C 語言標準整數型別在不同平台大小可能不同,資料庫常用固定大小型別名稱。以下為常見整數型別範圍與位元組大小對照 (以 32/64 位系統常見實作為例):
| 型別名稱 | 範圍 | 位元組大小 | 備註 |
|---|---|---|---|
tinyint / uint8_t |
0 ~ 255 | 1 | 無號 8 位元,常用旗標、小計數 |
smallint / int16_t |
-32,768 ~ 32,767 | 2 | 有號 16 位元 |
int / int32_t |
-2,147,483,648 ~ 2,147,483,647 | 4 | 主要整數型別,integer 同義字 |
bigint / int64_t |
-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 | 8 | 超出 int 範圍時使用 |
| 有號 | 無號 | 範圍 (無號) | 大小 |
|---|---|---|---|
int8_t | uint8_t | 0 ~ 255 | 1 |
int16_t | uint16_t | 0 ~ 65,535 | 2 |
int32_t | uint32_t | 0 ~ 4,294,967,295 | 4 |
int64_t | uint64_t | 0 ~ 18,446,744,073,709,551,615 | 8 |
<stdint.h> (C99) 提供 intN_t, uintN_t,跨平台行為一致int/long 做序列化/網路傳輸:大小隨平台變化 (LP64 vs LLP64)bigint=8 bytes, int=4, smallint=2, tinyint=1;MySQL/PostgreSQL 同bigint,函數回傳 bigint 需參數為 bigint123L (long), 123LL (long long), 123U (unsigned), 123ULL| 資料模型 | int | long | long long | pointer | 典型系統 |
|---|---|---|---|---|---|
| LP64 | 4 | 8 | 8 | 8 | Linux/macOS 64-bit |
| LLP64 | 4 | 4 | 8 | 8 | Windows 64-bit |
| ILP32 | 4 | 4 | 8 | 4 | 32-bit 系統 |
結論:跨平台程式碼務必使用 int32_t, int64_t 等固定寬度型別,避免依賴 long 大小。