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

本文介绍了C/C++中uint8_t、uint16_t、uint32_t、uint64_t、size_t和ssize_t等数据类型,它们实际上是通过typedef创建的类型别名。这些类型在C99标准中被定义,用于提供不同字节数的无符号整型,方便跨平台代码的维护。文章还提到了_int_t后缀的含义以及在不同平台下如何使用这些类型。
摘要由CSDN通过智能技术生成
原文写的不错,转来收藏,转自: 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)

  1. bash-3.00$ vi int_types.h
  2. "int_types.h" [Read only] 176 lines, 4367 characters
  3. /*
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * Use is subject to license terms.
  6. */
  7. #ifndef _SYS_INT_TYPES_H
  8. #define _SYS_INT_TYPES_H
  9. #pragma ident "@(#)int_types.h 1.10 04/09/28 SMI"
  10. /*
  11. * This file, <sys/int_types.h>, is part of the Sun Microsystems implementation
  12. * of <inttypes.h> defined in the ISO C standard, ISO/IEC 9899:1999
  13. * Programming language - C.
  14. *
  15. * Programs/Modules should not directly include this file. Access to the
  16. * types defined in this file should be through the inclusion of one of the
  17. * following files:
  18. *
  19. * <sys/types.h> Provides only the "_t" types defined in this
  20. * file which is a subset of the contents of
  21. * <inttypes.h>. (This can be appropriate for
  22. * all programs/modules except those claiming
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值