热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c/c++:uint8_tuint16_tuint32_tuint64_tsize_tssize_t数据类型

原文写的不错,转来收藏,转自:http:wangyisouhuxin.blog.163.comblogstatic761966592011072348700?fromdm&am
 原文写的不错,转来收藏,转自: 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, , is part of the Sun Microsystems implementation
 * of  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:
 *
 *                 Provides only the "_t" types defined in this
 *                              file which is a subset of the contents of
 *                              .  (This can be appropriate for
 *                              all programs/modules except those claiming
 *                              ANSI-C conformance.)
 *
 *              Provides the Kernel and Driver appropriate
 *                              components of .
 *
 *                  For use by applications.
 *
 * See these files for more details.
 */

#include 

#ifdef __cplusplus
extern "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
#endif
typedef short                   int16_t;
typedef int                     int32_t;
#ifdef  _LP64
#define _INT64_TYPE
typedef long                    int64_t;
#else   /* _ILP32 */
#if defined(_LONGLONG_TYPE)
#define _INT64_TYPE
typedef long long               int64_t;
#endif
#endif

typedef unsigned char           uint8_t;
typedef unsigned short          uint16_t;
typedef unsigned int            uint32_t;
#ifdef  _LP64
typedef 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;
#else
typedef 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;
#else
typedef 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
#endif
typedef int                     int_fast16_t;
typedef int                     int_fast32_t;
#ifdef  _LP64
typedef long                    int_fast64_t;
#else   /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef long long               int_fast64_t;
#endif
#endif

typedef unsigned char           uint_fast8_t;
typedef unsigned int            uint_fast16_t;
typedef unsigned int            uint_fast32_t;
#ifdef  _LP64
typedef 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
#endif
typedef short                   int_least16_t;
typedef int                     int_least32_t;
#ifdef  _LP64
typedef long                    int_least64_t;
#else   /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef long long               int_least64_t;
#endif
#endif

typedef unsigned char           uint_least8_t;
typedef unsigned short          uint_least16_t;
typedef unsigned int            uint_least32_t;
#ifdef  _LP64
typedef 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的定义,篇幅较长不贴出了。


推荐阅读
  • 部分转载自:http:blog.csdn.netliujiuxiaoshitouarticledetails69920917头文件#include<assert.h& ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • AtonepointIhadlookedatimplementingaclasstemplateinC++thatwouldsupportanEnumthatwo ... [详细]
  • Igotthiscode(IknowitsinSpanishIcantranslateifneeded)wheretheygivemethefunctionS ... [详细]
  • ProblemA.MonsterPathCodejamonisamobilegameinwhichmonstertrainer ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文介绍了最长上升子序列问题的一个变种解法,通过记录拐点的位置,将问题拆分为左右两个LIS问题。详细讲解了算法的实现过程,并给出了相应的代码。 ... [详细]
  • STL学习笔记--数值算法
    数值算法  C++STL的数值算法(Numericalgorithms)是一组对容器元素进行数值计算的模板函数,包括容器元素求和accumulate、两序列元素的内积inner_pro ... [详细]
author-avatar
美猴qing_243
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有