你所不知道的C

1.为什么要用uint8_t,uint16_t,uint32_t数据类型

    我们都知道linux C开发中的常见扩展数据类型的定义有:uint8_t, uint16_t, uint32_t, uint64_t, size_t, ssize_t, off_t .... 他之所以要自己定义出数据类型是有道理的, 如: typdef unsigned int uint32_t; 表示uint32_t为32位无符号类型数据, 其实size_t也是32位无符号数据类型, 为什么不直接写"unsigned int"呢?
    为了程序的可扩展性, 假如将来我们需要的数据大小变成了64bit时,我们只需要将typedef long int size_t就可以了, 不然我们可要修改好多好多的地方了. 这种设计我们同样可以应用到自己的开发中来,当自己设计一个int类型保存某种数据时,但你又没把握将来是不是要用long int时你可以引用一个自己定义的数据类型的啊!

  在nesc(nesC 是对 C 的扩展,它基于体现TinyOS 的结构化概念和执行模型而设计 。 TinyOS 是为传感器网络节点而设计的一个事件驱动的操作系统,传感器网络节点拥有非常有限的资源 ( 举例来说., 8K 字节的程序储存器,512个字节的随机存取储存器) 。TinyOS 用 nesC 重新编写。参见参考文献1的代码中,你会看到很多你不认识的数据类型,比如uint8_t等。咋一看,好像是个新的数据类型,不过C语言(nescC的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的疑问。论坛上就有人问:以*_t结尾的类型是不是都是long型的?在baidu上查一下,才找到答案,这时才发觉原来自己对C掌握的太少。

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

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

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

 按照posix标准(POSIX是Portable Operating System Interface of Unix的缩写。由IEEE(Institute of Electrical and Electronic Engineering)开发,由ANSI和ISO标准化。参见参考文献2.),一般整形对应的*_t类型为:
1字节     uint8_t
2
字节     uint16_t
4
字节     uint32_t
8
字节     uint64_t

附:C99标准中inttypes.h的内容
00001 /*
00002    inttypes.h
00003 
00004    Contributors:
00005      Created by Marek Michalkiewicz <marekm@linux.org.pl>
00006 
00007    THIS SOFTWARE IS NOT COPYRIGHTED
00008 
00009    This source code is offered for use in the public domain.  You may
00010    use, modify or distribute it freely.
00011 
00012    This code is distributed in the hope that it will be useful, but
00013    WITHOUT ANY WARRANTY.  ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
00014    DISCLAIMED.  This includes but is not limited to warranties of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  */
00017 
00018 #ifndef __INTTYPES_H_
00019 #define __INTTYPES_H_
00020 
00021 /* Use [u]intN_t if you need exactly N bits.
00022    XXX - doesn't handle the -mint8 option.  */
00023 
00024 typedef signed char int8_t;
00025 typedef unsigned char uint8_t;
00026 
00027 typedef int int16_t;
00028 typedef unsigned int uint16_t;
00029 
00030 typedef long int32_t;
00031 typedef unsigned long uint32_t;
00032 
00033 typedef long long int64_t;
00034 typedef unsigned long long uint64_t;
00035 
00036 typedef int16_t intptr_t;
00037 typedef uint16_t uintptr_t;
00038 
00039 #endif

参考文献:1.http://baike.baidu.com/view/2021179.htm

2.http://baike.baidu.com/view/209573.htm

3.http://peipengshuai.blog.163.com/blog/static/19012266201212313542392/

4.http://www.cnblogs.com/wwping/articles/2293898.html

原文地址:https://www.cnblogs.com/xlw1219/p/2821352.html