当前位置:网站首页>[revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
[revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
2022-06-30 11:42:00 【AI is very good】
Contents of this chapter :
1. c In language %x、%c、%d、%x And so on.
%a,%A Read in a floating point value
%c Read in a character
%d Read in decimal integers
%i Read in decimal , octal , Hexadecimal integer
%o Read in octal integers
%x,%X Read in hexadecimal integers
%s Read in a string , In case of space 、 End with tab or newline .
%f,%F,%e,%E,%g,%G It's used to enter real numbers , You can enter it in decimal or exponential form .
%p Read in a pointer
%u Read in an unsigned decimal integer
%n The number of equivalent characters of the value read so far
%[] Scan character sets
%% read % Symbol
2. c Language address character & The role of
C Language address character & There are two common places for .
- Pointer variable assignment
int m = 214;
int *i = &m;
- In the formal parameter of a function, it appears before the formal parameter variable
void func(int* &i)
{
}
3. understand C In language Consortium
3.1 What is a consortium ?
stay C In language , The definition of variable is the process of allocating storage space . General , Each variable has its own storage space , So can different data types be stored in the same memory space ( Not stored at the same time ) Well ?
The answer is yes , This can be achieved by using a consortium . A consortium is also called a consortium , stay C The key words defining a consortium in the language are union.
3.2 Definition
The general form of defining a union type is :
union Union name
{
Membership table
};
The member table contains several members , The general form of membership is : Type specifier Member name . The number of bytes it occupies is the same as the number of bytes occupied by the largest data type in the member .
3.2.1 give an example
Method 1 : Create a template first , Redefining variables
// Create a consortium template union perdata
union perdata
{
int Class;
char Office;
};
// Use the consortium template to create two variables a, b
union perdata a,b;
here ,perdata Is the name of the consortium , The name was chosen by us at will , But try to give it a meaningful name . It is equivalent to a template , You can use this template to define variables a、b. Don't forget to define union.
Method 2 : Create templates and variables at the same time
// Create a consortium template union perdata Define two variables at the same time a、b
union perdata
{
int Class;
char Office;
}a,b;
This is similar to method one .
Method 3 : Omit the consortium name
union
{
int Class;
char Office;
}a,b;
Relative to method one and method two , The consortium name is omitted here . Although more concise , But because there is no name , You can't use this union to define new variables later .
Method four : Use typedef
// Consortium template union perdata Rename as perdata_U
typedef union perdata
{
int Class;
char Office;
}perdata_U;
// Use a new name perdata_U Create two variables a, b
perdata_U a,b;
3.3 Initialize the consortium
The initialization of the union is different from that of the structure , A union can only store one value . The consortium has three initialization methods :
perdata_U a;
a.Class = 10;
perdata_U b = a; /* 1、 Initialize a union to another union of the same type ; */
perdata_U c = {
20}; /* 2、 Initialize the first member of the Union ; */
perdata_U d = {
.Office = 30}; /* 3、 according to C99 standard , Use the specified initializer . */
3.4 application
Application 1 : Judge whether it is big end or small end
Understanding of big end and small end , Please move to this article , Portal
#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 It's low , When reading memory , From low address to high address , What is stored in the low address is the low order , So it's a small end
return 0;
}
Application 2 : Separate high and low bytes
The operation of separating high and low bytes is often encountered in single chip microcomputer , For example, the timer interrupt reset operation is often performed
(65535-200)/256,
(65535-200)%256
This kind of operation , One division consumes four machine cycles , Taking remainder also requires a series of complex operations , If such operations need to be performed many times in a short time, it will undoubtedly bring a huge burden to the program . In fact, all we need for these operations is the separation of high and low bytes of data , In this way, we can easily reduce this part of the cost by using the consortium .
Code :
union div
{
int n; // n Store the high and low byte data to be separated in
char a[2]; // stay keil c An integer in takes up two bytes ,char Take up a byte , therefore n And array a Same number of bytes
}test;
test.n = 65535-200; // After this sentence, it's all ok 了 , Let's visit test In the array a To fetch high and low byte data
TH1 = test.a[0]; // test.a[0] The high-order data is stored in
TL1 = test.a[1]; // test.a[1] Stored in test.n Low order data
Union data is aligned by address . Whether the high-order data or the low-order data depends on the size end mode of the platform ,51 It's the big end ,stm32 The default is small end , If there are other compilers, please test yourself . Division is achieved with only one subtraction instruction 、 The operation of taking remainder , It is especially useful for high frequency timing .
=========================================================================
reference
边栏推荐
- 100 important knowledge points that SQL must master: summary data
- 【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
- Esp32-c3 introductory tutorial basic part ⑪ - reading and writing non-volatile storage (NVS) parameters
- Digitalization is not a trial, but a wading out of "Xingzhi Digital China" × History of Foxconn
- 【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
- What is erdma as illustrated by Coptic cartoon?
- Flutter start from scratch 008 form
- Create - configure factory
- How to 'gracefully' avoid MySQL login prompt information in scripts
- Use of switch statement in go language learning
猜你喜欢
Database connection pool Druid
What is erdma as illustrated by Coptic cartoon?
“新数科技”完成数千万元A+轮融资,造一体化智能数据库云管理平台
Record the memory leak of viewpager + recyclerview once
8 lines of code to achieve quick sorting, easy to understand illustrations!
Object mapping - mapping Mapster
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
记一次ViewPager + RecyclerView的内存泄漏
科普達人丨漫畫圖解什麼是eRDMA?
Win10 R package installation error: not installed in arch=i386
随机推荐
R语言去重操作unique duplicate filter
A man is a Book
[applet practice series] Introduction to the registration life cycle of the applet framework page
建立自己的网站(13)
【西安交通大学】考研初试复试资料分享
CVPR 2022 | 大幅减少零样本学习所需人工标注,马普所和北邮提出富含视觉信息的类别语义嵌入...
“\“id\“ contains an invalid value“
1175. 质数排列
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
win10 R包安装报错:没有安装在arch=i386
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
QT embeds the sub QT program window into the current program
Record the memory leak of viewpager + recyclerview once
压缩状态DP位运算
深入解析 Apache BookKeeper 系列:第四篇—背压
数据库 事务
Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age
"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
ARouter 最新问题合集