1. 程式人生 > >淺析C語言之uint8_t / uint16_t / uint32_t /uint64_t

淺析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  版權宣告:本文為博主原創文章,轉載請附上博文連結!