浅析C语言之uint8_t / uint16_t / uint32_t /uint64_t

一、C语言基本数据类型回顾

在C语言中有6种基本数据类型:short、int、long、float、double、char

1、数值类型

1)整型:short、int、long

2)浮点型:float、double

2、字符类型:char

二、typedef回顾

typedef用来定义关键字或标识符的别名,例如:

typedef double wages;
typedef wages salary;

三、uint8_t\uint_16_t\uint32_t\uint64_t
1、这些类型的来源:这些数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。也就是说,它们其实是我们已知的类型的别名。

2、使用这些类型的原因:方便代码的维护。比如,在C中没有bool型,于是在一个软件中,一个程序员使用int,一个程序员使用short,会比较混乱。最好用一个typedef来定义一个统一的bool:

typedef char bool;

在涉及到跨平台时,不同的平台会有不同的字长,所以利用预编译和typedef可以方便的维护代码。

3、这些类型的定义:

在C99标准中定义了这些数据类型,具体定义在:/usr/include/stdint.h    ISO C99: 7.18 Integer types

#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  
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  

4、格式化输出:

uint16_t %hu
uint32_t %u
uint64_t %llu

5、uint8_t类型的输出:
注意uint8_t的定义为

typedef unsigned char           uint8_t;

uint8_t实际上是一个char。所以输出uint8_t类型的变量实际上输出其对应的字符,而不是数值。例:

uint8_t num = 67;
cout << num << endl;


输出结果:C
参考:

http://blog.sina.com.cn/s/blog_9dcc0fb90101gdvo.html

http://blog.csdn.net/mrlixirong/article/details/48416533

http://blog.csdn.net/kiddy19850221/article/details/6655066


--------------------- 
作者:海阔天空sky1992 
来源:CSDN 
原文:https://blog.csdn.net/Mary19920410/article/details/71518130 
版权声明:本文为博主原创文章,转载请附上博文链接!

发布了21 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Argon_Ghost/article/details/83443229