对于一些常用数据类型的理解
在上个项目的网络编程中,遇到了很多typedef定义的数据类型,现进行记录,主要参考了一下链接:
https://blog.csdn.net/EUSIA/article/details/76401235
https://www.cnblogs.com/curo0119/p/8891906.html
https://www.jb51.net/article/109690.htm
https://blog.csdn.net/yz930618/article/details/84785970
int_t同类
int_t 为一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是一种新的数据类型。因为跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以最有效的维护代码。
他们在stdint.h中定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29/* Signed. */ /* There is some amount of overlap with <sys/types.h> as known by inet code */ #ifndef __int8_t_defined # define __int8_t_defined typedef signed char int8_t; typedef short int int16_t; typedef int int32_t; # if __WORDSIZE == 64 typedef long int int64_t; # else __extension__ typedef long long int int64_t; # endif #endif /* Unsigned. */ typedef unsigned char uint8_t; typedef unsigned short int uint16_t; #ifndef __uint32_t_defined typedef unsigned int uint32_t; # define __uint32_t_defined #endif #if __WORDSIZE == 64 typedef unsigned long int uint64_t; #else __extension__ typedef unsigned long long int uint64_t; #endif
其表示的范围如下表所示:
Specifier | Common Equivalent | Signing | Bits | Bytes | Minimum Value | Maximum Value |
int8_t | signed char | signed | 8 | 1 | -128 | 128 |
uint8_t | unsigned char | unsigned | 8 | 1 | 0 | 255 |
int16_t | short | signed | 16 | 2 | -32768 | 32767 |
uint16_t | unsigned short | unsigned | 16 | 2 | 0 | 65535 |
int32_t | int | signed | 32 | 4 | -2147483648 | 2147482647 |
uint32_t | unsigned int | unsigned | 32 | 4 | 0 | 4294967295 |
int64_t | long long | signed | 64 | 8 | 9223372036854770000 | 9223372036854770000 |
uint64_t | unsigned long long | unsigned | 64 | 8 | 0 | 18446744073709500000 |
size_t与ssize_t
size_t主要用于计数,如sizeof函数返回值类型即为size_t。在不同位的机器中所占的位数也不同,size_t是无符号数,ssize_t是有符号数。
1
2
3在32位机器中定义为:typedef unsigned int size_t; (4个字节) 在64位机器中定义为:typedef unsigned long size_t;(8个字节)
由于size_t是无符号数,因此,当变量有可能为负数时,必须使用ssize_t。因为当有符号整型和无符号整型进行运算时,有符号整型会先自动转化成无符号。
此外,int 无论在32位还是64位机器中,都是4个字节, 且带符号,可见size_t与int 的区别之处。
各数据类型输入输出符
有时候做调试的时候,对于各种类型,其输出格式也会有所不同,主要如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14int8_t:%c; uint8_t:%c; int16_t: %hd; uint16_t:%hu; int32_t:%d; uint32_t:%u; int64_t:%l64d; uint64_t:%l64u; size_t:%zd;
最后
以上就是笨笨故事最近收集整理的关于对于一些常用数据类型的理解对于一些常用数据类型的理解的全部内容,更多相关对于一些常用数据类型内容请搜索靠谱客的其他文章。
发表评论 取消回复