[u]intN_t - uint8_t, uint16_t, uint32_t, uint64_t

Koma_Wong發表於2018-06-14

uint8_t, uint16_t, uint32_t, uint64_t

在c/c++中,很多以_t結尾的資料型別,如uint8_t,size_t等等,乍一看什麼鬼,實際上_t的意思就是typedef的字尾縮寫。具體如下:

C99標準中inttypes.h的內容

/*
   inttypes.h: C99標準中inttypes.h的內容

   Contributors:
     Createdby Marek Michalkiewicz <marekm@linux.org.pl>

   THISSOFTWARE IS NOT COPYRIGHTED

   Thissource code is offered for use in the public domain.  You may
   use,modify or distribute it freely.

   Thiscode is distributed in the hope that it will be useful, but
   WITHOUTANY WARRANTY.  ALLWARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
   DISCLAIMED.  This includes but is not limited towarranties of
   MERCHANTABILITYor FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef __INTTYPES_H_
#define __INTTYPES_H_

/* Use [u]intN_t if you need exactly N bits.
  XXX- doesn't handle the -mint8 option.  */

typedef signed char int8_t;
typedef unsigned char uint8_t;

typedef int int16_t;
typedef unsigned int uint16_t;
 
typedef long int32_t;
typedef unsigned long uint32_t;

typedef long long int64_t;
typedef unsigned long long uint64_t;

typedef int16_t intptr_t;
typedef uint16_t uintptr_t;
 
#endif

C99標準另一個檔案/usr/include/stdint.h內如下:

/*在C99標準中定義了這些資料型別,
  具體定義在:/usr/include/stdint.h
  參考:https://blog.csdn.net/Mary19920410/article/details/71518130?locationNum=4&fps=1*/
#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  

相關文章