c/c++: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

来源:互联网 发布:js飞机大战源代码 编辑:程序博客网 时间:2024/05/18 01:27
 原文写的不错,转来收藏,转自:http://wangyisouhuxin.blog.163.com/blog/static/761966592011072348700/?fromdm&fromSearch&isFromSearchEngine=yes

 

在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等。咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的疑问。论坛上就有人问:以*_t结尾的类型是不是都是long型的?在baidu上查一下,才找到答案,这时才发觉原来自己对C掌握的太少。

那么_t的意思到底表示什么?具体的官方答案没有找到,不过我觉得有个答案比较接近。它就是一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是其它数据类型。

uint8_t,uint16_t,uint32_t等都不是什么新的数据类型,它们只是使用typedef给类型起的别名,新瓶装老酒的把戏。不过,不要小看了typedef,它对于你代码的维护会有很好的作用。比如C中没有bool,于是在一个软件中,一些程序员使用int,一些程序员使用short,会比较混乱,最好就是用一个typedef来定义,如:
typedef char bool;

一般来说,一个C的工程中一定要做一些这方面的工作,因为你会涉及到跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以让你最有效的维护你的代码。为了用户的方便,C99标准的C语言硬件为我们定义了这些类型,我们放心使用就可以了。

按照posix标准,一般整形对应的*_t类型为:
       1字节     uint8_t
       2字节     uint16_t
       4字节     uint32_t
       8字节     uint64_t

附:inttypes.h的内容(不同的服务器会有不同的源文件结构,但原理是一样的,我这里sun服务器inttypes.h引用了int_type.h)

bash-3.00$ vi int_types.h "int_types.h" [Read only] 176 lines, 4367 characters /* * Copyright 2004 Sun Microsystems, Inc.  All rights reserved. * Use is subject to license terms. */#ifndef _SYS_INT_TYPES_H#define _SYS_INT_TYPES_H#pragma ident   "@(#)int_types.h        1.10    04/09/28 SMI"/* * This file, <sys/int_types.h>, is part of the Sun Microsystems implementation * of <inttypes.h> defined in the ISO C standard, ISO/IEC 9899:1999 * Programming language - C. * * Programs/Modules should not directly include this file.  Access to the * types defined in this file should be through the inclusion of one of the * following files: * *      <sys/types.h>           Provides only the "_t" types defined in this *                              file which is a subset of the contents of *                              <inttypes.h>.  (This can be appropriate for *                              all programs/modules except those claiming *                              ANSI-C conformance.) * *      <sys/inttypes.h>        Provides the Kernel and Driver appropriate *                              components of <inttypes.h>. * *      <inttypes.h>            For use by applications. * * See these files for more details. */#include <sys/feature_tests.h>#ifdef __cplusplusextern "C" {#endif/* * Basic / Extended integer types * * The following defines the basic fixed-size integer types. * * Implementations are free to typedef them to Standard C integer types or * extensions that they support. If an implementation does not support one * of the particular integer data types below, then it should not define the * typedefs and macros corresponding to that data type.  Note that int8_t * is not defined in -Xs mode on ISAs for which the ABI specifies "char" * as an unsigned entity because there is no way to define an eight bit * signed integral. */#if defined(_CHAR_IS_SIGNED)typedef char                    int8_t;#else#if defined(__STDC__)typedef signed char             int8_t;#endif#endiftypedef short                   int16_t;typedef int                     int32_t;#ifdef  _LP64#define _INT64_TYPEtypedef long                    int64_t;#else   /* _ILP32 */#if defined(_LONGLONG_TYPE)#define _INT64_TYPEtypedef long long               int64_t;#endif#endiftypedef unsigned char           uint8_t;typedef unsigned short          uint16_t;typedef unsigned int            uint32_t;#ifdef  _LP64typedef unsigned long           uint64_t;#else   /* _ILP32 */#if defined(_LONGLONG_TYPE)typedef unsigned long long      uint64_t;#endif#endif/* * intmax_t and uintmax_t are to be the longest (in number of bits) signed * and unsigned integer types supported by the implementation. */#if defined(_INT64_TYPE)typedef int64_t                 intmax_t;typedef uint64_t                uintmax_t;#elsetypedef int32_t                 intmax_t;typedef uint32_t                uintmax_t;#endif/* * intptr_t and uintptr_t are signed and unsigned integer types large enough * to hold any data pointer; that is, data pointers can be assigned into or * from these integer types without losing precision. */#if defined(_LP64) || defined(_I32LPx)typedef long                    intptr_t;typedef unsigned long           uintptr_t;#elsetypedef int                     intptr_t;typedef unsigned int            uintptr_t;#endif/* * The following define the fastest integer types that can hold the * specified number of bits. */#if defined(_CHAR_IS_SIGNED)typedef char                    int_fast8_t;#else#if defined(__STDC__)typedef signed char             int_fast8_t;#endif#endiftypedef int                     int_fast16_t;typedef int                     int_fast32_t;#ifdef  _LP64typedef long                    int_fast64_t;#else   /* _ILP32 */#if defined(_LONGLONG_TYPE)typedef long long               int_fast64_t;#endif#endiftypedef unsigned char           uint_fast8_t;typedef unsigned int            uint_fast16_t;typedef unsigned int            uint_fast32_t;#ifdef  _LP64typedef unsigned long           uint_fast64_t;#else   /* _ILP32 */#if defined(_LONGLONG_TYPE)typedef unsigned long long      uint_fast64_t;#endif#endif/* * The following define the smallest integer types that can hold the * specified number of bits. */#if defined(_CHAR_IS_SIGNED)typedef char                    int_least8_t;#else#if defined(__STDC__)typedef signed char             int_least8_t;#endif#endiftypedef short                   int_least16_t;typedef int                     int_least32_t;#ifdef  _LP64typedef long                    int_least64_t;#else   /* _ILP32 */#if defined(_LONGLONG_TYPE)typedef long long               int_least64_t;#endif#endiftypedef unsigned char           uint_least8_t;typedef unsigned short          uint_least16_t;typedef unsigned int            uint_least32_t;#ifdef  _LP64typedef unsigned long           uint_least64_t;#else   /* _ILP32 */#if defined(_LONGLONG_TYPE)typedef unsigned long long      uint_least64_t;#endif#endif#ifdef __cplusplus}#endif#endif /* _SYS_INT_TYPES_H */

同理在types.h文件中会有size_t ssize_t的定义,篇幅较长不贴出了。

原创粉丝点击