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

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

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

/* 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是有符号数。

在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 的区别之处。

各数据类型输入输出符

有时候做调试的时候,对于各种类型,其输出格式也会有所不同,主要如下所示:

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;
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值