浅析C语言之uint8_t / uint16_t / uint32_t /uint64_t

本文详细介绍了C语言中的基本数据类型,包括整型、浮点型、字符型等,并深入探讨了typedef的作用及其如何用于定义统一的数据类型别名。特别地,文章重点解释了uint8_t、uint16_t、uint32_t、uint64_t等类型的定义及使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、C语言基本数据类型回顾

在C语言中有6种基本数据类型:short、int、long、float、double、char

1、数值类型

1)整型:short、int、long

2)浮点型:float、double

2、字符类型:char

二、typedef回顾

typedef用来定义关键字或标识符的别名,例如:

typedef double wages;
typedef wages salary;
三、uint8_t\uint_16_t\uint32_t\uint64_t

1、这些类型的来源:这些数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。也就是说,它们其实是我们已知的类型的别名。

2、使用这些类型的原因:方便代码的维护。比如,在C中没有bool型,于是在一个软件中,一个程序员使用int,一个程序员使用short,会比较混乱。最好用一个typedef来定义一个统一的bool:

typedef char bool;

在涉及到跨平台时,不同的平台会有不同的字长,所以利用预编译和typedef可以方便的维护代码。

3、这些类型的定义:

在C99标准中定义了这些数据类型,具体定义在:/usr/include/stdint.h      ISO C99: 7.18 Integer types

#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  

4、格式化输出:

uint16_t %hu
uint32_t %u
uint64_t %llu
5 、uint8_t类型的输出:

注意uint8_t的定义为

typedef unsigned char           uint8_t;
uint8_t实际上是一个char。所以输出uint8_t类型的变量实际上输出其对应的字符,而不是数值。例:

uint8_t num = 67;
cout << num << endl;
输出结果:C

参考:

http://blog.sina.com.cn/s/blog_9dcc0fb90101gdvo.html

http://blog.csdn.net/mrlixirong/article/details/48416533

http://blog.csdn.net/kiddy19850221/article/details/6655066



`of_device_id`是Linux内核中用于匹配设备树节点和驱动程序的结构体。该结构体包含了设备树节点的一些属性信息,如设备类型、厂商信息、设备名称、设备ID等,以及一个与之对应的驱动程序名称或ID,用于在设备树中查找匹配的节点。 以下是`of_device_id`结构体的定义: ```c struct of_device_id { char name[32]; // 设备名称 char type[32]; // 设备类型 const void *data; // 与设备匹配的驱动程序信息 }; ``` `of_device_id`结构体通常作为驱动程序的一部分,在驱动程序初始化时被使用。驱动程序会调用`of_match_device()`函数来根据`of_device_id`结构体中的信息,在设备树中查找匹配的节点,并将其与驱动程序绑定。在匹配成功后,驱动程序可以通过`of_device`结构体获得这个节点的一些属性信息。 例如,以下是一个`of_device_id`结构体的示例: ```c static const struct of_device_id my_driver_of_match[] = { { .compatible = "my-device" }, { /* end of list */ }, }; ``` 在驱动程序初始化时,可以使用`MODULE_DEVICE_TABLE(of, my_driver_of_match)`宏将该`of_device_id`结构体与驱动程序关联起来。然后,内核会自动在设备树中查找`compatible`属性为`"my-device"`的节点,并将其与驱动程序绑定。 总之,`of_device_id`结构体是Linux内核中用于匹配设备树节点和驱动程序的重要机制,它可以让驱动程序自动识别和绑定设备树节点,并简化驱动程序的开发。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值