我是靠谱客的博主 笨笨故事,这篇文章主要介绍对于一些常用数据类型的理解对于一些常用数据类型的理解,现在分享给大家,希望可以做个参考。

对于一些常用数据类型的理解

在上个项目的网络编程中,遇到了很多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

其表示的范围如下表所示:

SpecifierCommon EquivalentSigningBitsBytesMinimum ValueMaximum Value
int8_tsigned charsigned81-128128
uint8_tunsigned charunsigned810255
int16_tshortsigned162-3276832767
uint16_tunsigned shortunsigned162065535
int32_tintsigned324-21474836482147482647
uint32_tunsigned intunsigned32404294967295
int64_tlong longsigned6489223372036854770000 9223372036854770000
uint64_tunsigned long longunsigned648018446744073709500000

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
14
int8_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;

最后

以上就是笨笨故事最近收集整理的关于对于一些常用数据类型的理解对于一些常用数据类型的理解的全部内容,更多相关对于一些常用数据类型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部