当前位置:网站首页>【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
2022-06-30 11:06:00 【AI很不错呦】
本章目录:
1. c语言中%x、%c、%d、%x等等等
%a,%A 读入一个浮点值
%c 读入一个字符
%d 读入十进制整数
%i 读入十进制,八进制,十六进制整数
%o 读入八进制整数
%x,%X 读入十六进制整数
%s 读入一个字符串,遇空格、制表符或换行符结束。
%f,%F,%e,%E,%g,%G 用来输入实数,可以用小数形式或指数形式输入。
%p 读入一个指针
%u 读入一个无符号十进制整数
%n 至此已读入值的等价字符数
%[] 扫描字符集合
%% 读%符号
2. c语言取地址符&的作用
C语言取地址符&的有两个常用的地方。
- 指针变量赋值
int m = 214;
int *i = &m;
- 在函数的形式参数中出现在形参变量的前面
void func(int* &i)
{
}
3. 了解 C语言中的 联合体
3.1 什么是联合体?
在C语言中,变量的定义是分配存储空间的过程。一般的,每个变量都具有其独有的存储空间,那么可不可以在同一个内存空间中存储不同的数据类型(不是同时存储)呢?
答案是可以的,使用联合体就可以达到这样的目的。联合体也叫共用体,在C语言中定义联合体的关键字是union。
3.2 定义
定义一个联合类型的一般形式为:
union 联合名
{
成员表
};
成员表中含有若干成员,成员的一般形式为: 类型说明符 成员名。其占用的字节数与成员中最大数据类型占用的字节数。
3.2.1 举例
方法一:先创建模板,再定义变量
// 创建联合体模板union perdata
union perdata
{
int Class;
char Office;
};
// 使用该联合体模板创建两个变量a, b
union perdata a,b;
此处,perdata是联合体名,该名字是由我们任意定的,但是尽量起个有意义的名称。其相当于一个模板,可以使用这个模板去定义变量a、b。定义的时候不要忘了union。
方法二:同时创建模板和变量
// 创建联合体模板union perdata的同时定义两个变量a、b
union perdata
{
int Class;
char Office;
}a,b;
这与方法一差不多。
方法三:省略联合体名
union
{
int Class;
char Office;
}a,b;
相对于方法一与方法二,此处省略了联合体名。虽然更简洁了,但是因为没有了名字,后面就不能用该联合体定义新的变量。
方法四:使用typedef
// 联合体模板union perdata重新命名为perdata_U
typedef union perdata
{
int Class;
char Office;
}perdata_U;
// 使用新名字perdata_U创建两个变量a, b
perdata_U a,b;
3.3 初始化联合体
联合体的初始化与结构体不同,联合体只能存储一个值。联合体有三种初始化方法:
perdata_U a;
a.Class = 10;
perdata_U b = a; /* 1、把一个联合初始化为另一个同类型的联合; */
perdata_U c = {
20}; /* 2、初始化联合的第一个成员; */
perdata_U d = {
.Office = 30}; /* 3、根据C99标准,使用指定初始化器。 */
3.4 应用
应用一: 判断是大端还是小端
关于大端小端的理解,请移步至这篇文章,传送门
#include<stdio.h>
#include <stdbool.h>
union Utest
{
short num;
char c;
};
int main(void)
{
union Utest u;
u.num = 0x5566;
printf("%x\n",u.c);//66,66是低位,读取内存时,是从低地址到高地址,低地址中存储的是低位,因此是小端
return 0;
}
应用二:分离高低字节
单片机中经常会遇见分离高低字节的操作,比如进行计时中断复位操作时往往会进行
(65535-200)/256,
(65535-200)%256
这样的操作,而一个除法消耗四个机器周期,取余也需要进行一系列复杂的运算,如果在短时间内需要进行很多次这样的运算无疑会给程序带来巨大的负担。其实进行这些操作的时候我们需要的仅仅是高低字节的数据分离而已,这样利用联合体我们很容易降低这部分开销。
代码:
union div
{
int n; // n中存放要进行分离高低字节的数据
char a[2]; // 在keil c中一个整形占两个字节,char占一个字节,所以n与数组a占的字节数相同
}test;
test.n = 65535-200; // 进行完这句后就一切ok了,下面通过访问test中数组a的数据来取出高低字节的数据
TH1 = test.a[0]; // test.a[0]中存储的是高位数据
TL1 = test.a[1]; // test.a[1]中储存了test.n的低位数据
联合体内数据是按地址对齐的。具体是高位数据还是低位数据要看平台的大小端模式,51是大端,stm32默认是小端,如果其他编译器还请自测。仅仅用了一条减法指令就达到了除法、取余的操作,在进行高频率定时时尤为有用。
=========================================================================
参考文献
边栏推荐
- 10 reasons for system performance failure
- 100 important knowledge points that SQL must master: using subquery
- Line generation (Gauss elimination method, linear basis)
- 创建型-配置工厂
- 考研这些“不靠谱”的经验有多害人?
- SQL必需掌握的100个重要知识点:插入数据
- Multiparty Cardinality Testing for Threshold Private Set-2021:解读
- Set up your own website (13)
- 华三交换机清空配置
- Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality
猜你喜欢

Database connection pool Druid

Pointdistiller: structured knowledge distillation for efficient and compact 3D detection

Handler-源码分析

Summer vacation study record
![[xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations

OceanBase 安装 yum 源配置错误及解决办法

The first China Digital Collection conference will be held soon

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

How harmful are these "unreliable" experiences in the postgraduate entrance examination?

19年来最艰难的618,徐雷表达三个谢意
随机推荐
The latest collection of arouter problems
SQL必需掌握的100个重要知识点:分组数据
10天学会flutter DAY10 flutter 玩转 动画与打包
Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality
孔松(信通院)-数字化时代云安全能力建设及趋势
建立自己的网站(13)
SQL必需掌握的100个重要知识点:创建和操纵表
Train an image classifier demo in pytorch [learning notes]
Esp32-c3 introductory tutorial question ⑨ - core 0 panic 'ed (load access fault) Exception was unhandled. vfprintf. c:1528
关于IP定位查询接口的测评Ⅲ
Jetpack Compose DropdownMenu跟随手指点击位置显示
Introduction to China Mobile oneos development board
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
8 lines of code to achieve quick sorting, easy to understand illustrations!
100 important knowledge points that SQL must master: grouping data
盘点那些具有特色的写作软件
【无标题】
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information
Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court
记一次ViewPager + RecyclerView的内存泄漏