当前位置:网站首页>[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
边栏推荐
- Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?
- Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market
- Alibaba cloud database represented by polardb ranks first in the world
- Xu Lei expressed three thanks for the most difficult 618 in 19 years
- 100 important knowledge points that SQL must master: using table aliases
- 限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
- [leetcode 16] sum of three numbers
- Uncover the whole link communication process of customer service im
- Filter error in dplyr: can't transform a data frame with duplicate names
- Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
猜你喜欢

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

数字化不是试出来,而是蹚出来的|行知数字中国 × 富士康史喆

暑假学习记录

Object mapping - mapping Mapster

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

PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information

EMC surge

Summer vacation study record

【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
随机推荐
Typescript readonlyarray (read only array type) details
100 important knowledge points that SQL must master: insert data
暑假学习记录
科普達人丨漫畫圖解什麼是eRDMA?
华三交换机清空配置
EMC-浪涌
Filter error in dplyr: can't transform a data frame with duplicate names
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
R language view version R package view version
【模式识别大作业】
Summer vacation study record
建立自己的网站(13)
R语言查看版本 R包查看版本
It's time for the kotlin coroutine to schedule thread switching to solve the mystery
Pointdistiller: structured knowledge distillation for efficient and compact 3D detection
100 important knowledge points that SQL must master: using table aliases
A theoretical defect of relative position coding transformer and Its Countermeasures
科普达人丨漫画图解什么是eRDMA?
100 important knowledge points that SQL must master: updating and deleting data
Webview,ScrollView滑动冲突咋整