当前位置:网站首页>[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
边栏推荐
- 记一次ViewPager + RecyclerView的内存泄漏
- Mathematics (fast power)
- CVPR 2022 | 大幅减少零样本学习所需人工标注,马普所和北邮提出富含视觉信息的类别语义嵌入...
- R language view version R package view version
- 科普达人丨漫画图解什么是eRDMA?
- ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528
- Compression state DP bit operation
- Database transactions
- How to 'gracefully' avoid MySQL login prompt information in scripts
- 阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
猜你喜欢

数学(快速幂)

HMS core audio editing service 3D audio technology helps create an immersive auditory feast

优惠券种类那么多,先区分清楚再薅羊毛!

记一次ViewPager + RecyclerView的内存泄漏

“新数科技”完成数千万元A+轮融资,造一体化智能数据库云管理平台

STM32F407ZGT6使用SDIO方式驱动SD卡

R language de duplication operation unique duplicate filter

他是上海两大产业的第一功臣,却在遗憾中默默离世

据说用了这个,老板连夜把测试开了

1175. 质数排列
随机推荐
Introduction to game theory
Uncover the whole link communication process of customer service im
promise async和await的方法与使用
Introduction to China Mobile oneos development board
QT embeds the sub QT program window into the current program
EMC surge
Is the golden cycle of domestic databases coming?
Set up your own website (13)
记一次ViewPager + RecyclerView的内存泄漏
华三交换机清空配置
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
[IC5000 tutorial] - 01- use daqdea graphical debug to debug C code
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
How to analyze native crash through GDB
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
一个人就是一本书
自定义一个注解来获取数据库的链接
科普达人丨漫画图解什么是eRDMA?
100 important knowledge points that SQL must master: Combined Query
数据库 事务