> For the complete documentation index, see [llms.txt](https://labspc.gitbook.io/cnippets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://labspc.gitbook.io/cnippets/chap2.-lei-xing-yun-suan-fu-yu-biao-da-shi/2.2-shu-ju-lei-xing-ji-chang-du.md).

# 2.2 数据类型及长度

补充：默认情况下 ，c语言中的整型变量都是有符号的，也就是说最左位保留为符号位。无符号整数主要用于系统编程和底层与机器有关的应用，具体应用后文再提，这里回避讨论无符号整数。

书中只列举了四种数据类型：

* char
* int
* float
* double

关于这些类型长度定义，以及其他与机器和编译器有关的属性，可以在：\<limits.h> 和 \<float.h> 中找到。

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705655710-word-image-279-1.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705655712-word-image-279-2.png" alt=""><figcaption></figcaption></figure>

{% code title="2.2 signed-and-unsigned.c" lineNumbers="true" %}

```c
#include <stdio.h>
#include <limits.h>

int mian()
{
    printf("signed char min = %d\n", SCHAR_MIN);
    printf("signed char max = %d\n", SCHAR_MAX);
    printf("unsigned char max = %d\n", UCHAR_MAX);
    printf("signed short min = %d\n", SHRT_MIN);
    printf("signed short max = %d\n", SHRT_MAX);
    printf("unsigned short max = %d\n", USHRT_MAX);
    printf("signed int min = %d\n", INT_MIN);
    printf("signed int max = %d\n", INT_MAX);
    printf("unsigned int max = %u\n", UINT_MAX);
    printf("signed long min = %ld\n", LONG_MIN);
    printf("signed long max = %ld\n", LONG_MAX);
    printf("unsigned long max = %lu\n", ULONG_MAX);
    return 0;
}
```

{% endcode %}

在 main 函数中，使用了一系列的 printf 函数来打印各种类型的整数的最小值和最大值。这些整数类型包括有符号和无符号的 char、short、int 和 long。

每个 printf 函数都接受一个格式字符串和一个或多个参数。格式字符串中的 %d、%u 和 %ld 是格式说明符，用于指定参数的类型和格式。%d 用于打印有符号的十进制整数，%u 用于打印无符号的十进制整数，%ld 用于打印有符号的长整数。

例如，printf("signed char min = %d\n", SCHAR\_MIN); 这行代码打印出有符号字符的最小值。SCHAR\_MIN 是一个预处理宏，定义在 \<limits.h> 头文件中，表示有符号字符的最小值。

{% code title="" lineNumbers="true" %}

```c
➜  signed-and-unsigned ; exit;
signed char min = -128
signed char max = 127
unsigned char max = 255
signed short min = -32768
signed short max = 32767
unsigned short max = 65535
signed int min = -2147483648
signed int max = 2147483647
unsigned int max = 4294967295
signed long min = -9223372036854775808
signed long max = 9223372036854775807
unsigned long max = 18446744073709551615
```

{% endcode %}

第二种的方法是使用按位运算，后文再提。

### 2.2.1 为什么需要数据类型 <a href="#e11sb" id="e11sb"></a>

**从生活中去理解，为什么需要？** 小数，整数，qwert@，这种 只是举例。 怎么存储呢？

c 语言为了存储他们，进行了 “抽象”，来描述他们。

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705655714-word-image-279-3.png" alt=""><figcaption></figcaption></figure>

### 2.2.2 每一种类型的大小是多少 <a href="#clzmn" id="clzmn"></a>

知道大小的用处是什么？ 一个 bit 位里面，只能存 0 或者 1，空间大了，里面能存放的 xx个数 就能多些。空间越大，可以表示的数值的范围就越大，我们需要不同范围的数值的时候，我们就用不同类型来表示。

有关位与比特的知识，见 0.6 节。

{% code title="" lineNumbers="true" %}

```c
整型：
int //4 字节 byte 32bit
short //2 字节 byte 
long long //8 字节 byte
long //4 字节 byte

字符；
char //1 字节 byte

浮点：
float //4 字节 byte
double //8 字节 byte
```

{% endcode %}

### 2.2.3 为什么要出现这么多整型和浮点 <a href="#s7sdn" id="s7sdn"></a>

类型提供的丰富，使用上就会更加灵活，空间利用率也会更高些。这里提到空间利用上，这个挖一个小坑，后面来填。大小不同，表示范围就不同，就可以根据情况选择适当的类型。

{% code title="2.2.3 range-of-type.c" lineNumbers="true" %}

```c
#include <stdio.h>

int main() {
    printf("%lu\n", sizeof(char)); //1
    printf("%lu\n", sizeof(short));//2
    printf("%lu\n", sizeof(int));//
    printf("%lu\n", sizeof(long));
    printf("%lu\n", sizeof(long long));
    printf("%lu\n", sizeof(float));
    printf("%lu\n", sizeof(double));

    return 0;
}
```

{% endcode %}

```
➜ range-of-type ; exit;
1
2
4
8
8
4
8
```

### 2.2.4 c 语言类型怎么使用 <a href="#kh6qi" id="kh6qi"></a>

{% code title="2.2.4 use-type.c" lineNumbers="true" %}

```c
#include <stdio.h>

int main()
{
    //假如要存我的年龄22;我怎么存进去？
    int thisyear = 22;
    double mymacprice = 8700.5; //假设的数字
    return 0;
}
```

{% endcode %}

**类型是拿来创建变量的，创建变量的本质是向内存申请空间。**
