Files
CWithClasses/C/数据类型.md
2025-12-31 00:39:23 +08:00

373 lines
5.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

好,这个问题问得**非常对味** 👍
你现在这个阶段,就该把 **C 的类型系统一次性“扫干净”**,否则后面看 SDL / 源码 / 系统接口永远会被各种 `uint32_t``size_t``intptr_t` 绊住。
下面我给你一份 **「工程级·C语言数据类型大全」**,按**来源 + 本质**来分,保证你以后看到一个类型就知道它“从哪来、干嘛用、多大”。
---
# 一、C 语言内建基本类型(语言核心)
> 这是 **C 语法本体**,不需要 `#include`
## 1⃣ 整型Integer types
### 1字符整型
```c
char // 至少 8 位(实现定义)
signed char // 至少 8 位
unsigned char // 至少 8 位
```
⚠️ 注意:
* `char``signed char``unsigned char`
* `char` 是否有符号是**实现定义**
---
### 2短整型
```c
short
short int
signed short
signed short int
unsigned short
unsigned short int
```
* **至少 16 位**
* 常见16 位
---
### 3整型最常用
```c
int
signed int
unsigned int
```
* **至少 16 位**
* 常见:
* 32 位(现代系统)
---
### 4长整型
```c
long
long int
signed long
unsigned long
```
* **至少 32 位**
* 常见:
* Linux x6464 位
* Windows x6432 位LLP64
---
### 5长长整型C99
```c
long long
long long int
signed long long
unsigned long long
```
* **至少 64 位**
* 基本稳定为 64 位
---
## 2⃣ 浮点类型
```c
float // 单精度
double // 双精度
long double // 扩展精度(实现定义)
```
常见:
| 类型 | 位数 |
| ----------- | ------------- |
| float | 32 |
| double | 64 |
| long double | 80 / 128 / 64 |
---
## 3⃣ 布尔类型C99
```c
#include <stdbool.h>
bool // 实际是 _Bool
_Bool
```
* `_Bool` 是内建类型
* `bool` 是宏别名
---
## 4⃣ 空类型
```c
void
```
用途:
* 无返回值
* 泛型指针 `void*`
---
# 二、修饰符(类型“调味料”)
这些**不是独立类型**,而是修饰已有类型:
```c
signed
unsigned
short
long
```
合法组合例子:
```c
unsigned long long int
signed short
long double
```
非法组合:
```c
unsigned float
short double
```
---
# 三、固定宽度整数(`stdint.h`**强烈推荐**
> 你看到的 `int64_t`、`uint32_t` 全在这
```c
#include <stdint.h>
```
## 1⃣ 精确宽度(一定存在才定义)
```c
int8_t uint8_t
int16_t uint16_t
int32_t uint32_t
int64_t uint64_t
```
**最常用、最安全**
---
## 2⃣ 至少 N 位
```c
int_least8_t
int_least16_t
int_least32_t
int_least64_t
```
---
## 3⃣ 最快类型
```c
int_fast8_t
int_fast16_t
int_fast32_t
int_fast64_t
```
---
## 4⃣ 指针等宽整数(系统编程必备)
```c
intptr_t
uintptr_t
```
👉 能安全存放指针的整数
---
## 5⃣ 最大整数类型
```c
intmax_t
uintmax_t
```
---
# 四、常见 typedef标准库里“看起来像关键字的东西”
## 1⃣ `stddef.h`
```c
size_t // sizeof 的结果类型(无符号)
ptrdiff_t // 指针差值(有符号)
nullptr_t // C23
```
---
## 2⃣ `limits.h` / `stdint.h`
```c
CHAR_BIT // 一个字节多少 bit通常 8
INT_MAX
UINT_MAX
```
---
## 3⃣ `sys/types.h`POSIX
```c
ssize_t // size_t 的有符号版
off_t // 文件偏移
pid_t // 进程 ID
```
---
# 五、字符 & 文本相关类型
```c
char // 字节 / UTF-8
wchar_t // 宽字符(平台相关)
```
C11 还支持:
```c
char16_t
char32_t
```
(但在 C 里用得不多)
---
# 六、非标准但“到处都是”的别名(你问的 uint / uchar
⚠️ **这些不是 C 标准!**
常见于:
* Windows
* 嵌入式
* 老代码
* 游戏 / 引擎
```c
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned long ulong;
typedef long long int64;
typedef unsigned long long uint64;
```
👉 **完全是项目自定义**
👉 **千万别假设它们存在**
---
# 七、复合类型(不是基本类型,但必须知道)
```c
struct
union
enum
```
以及:
```c
typedef
```
---
# 八、函数 / 指针 / 数组“伪类型”
```c
int* p;
int (*fp)(int);
int arr[10];
```
它们不是新类型,而是**类型构造方式**
---
# 九、一张“终极速查表”(你该背的)
### ✅ 必背
* `char`
* `int`
* `long`
* `long long`
* `float`
* `double`
* `void`
* `size_t`
* `intptr_t`
* `int32_t / uint32_t`
* `int64_t / uint64_t`
* `bool`
### ❌ 不该依赖
* `uint`
* `uchar`
* `DWORD`
* `BYTE`
---
## 最后一句工程建议(很重要)
> **写现代 C**
>
> * 用 `stdint.h`
> * 用 `size_t`
> * 少猜位宽
> * 不造轮子 typedef
如果你愿意,下一步我可以帮你做一件事之一:
* 🔍 把 **SDL 里所有 typedef 拆给你看**
* 🧠 给你出 **“看到类型 → 推断平台行为”训练题**
* 🧱 教你 **自己写一个干净的 `types.h`**
你选一个,我接着往下带你。