当前位置:网站首页>Learn C language well from keywords
Learn C language well from keywords
2022-06-11 06:14:00 【Desperate Azi】

Catalog
3、 The most wronged keyword ——sizeof
Deeply understand the storage and retrieval of variable content
1、C Language data type

I'm learning C Before the language data type , We have to understand : Why there should be types ? Why are there so many types ?
① Why there should be types ?
answer : The essence of type is to rationalize the division of memory , On demand
② Why are there so many types ?
answer : Different application scenarios , Solve the different calculation methods corresponding to application scenarios , Different sizes of space are required . ( The essence : With minimum cost , Solve various scenarios )
After understanding these two problems, we will know Variable definitions : What is it? ? Why? ? What do I do ?
- The variable definition is : Open up a space in memory ( notes : On demand )
- Why define variables : Because we have to solve various scenarios
- What to do after defining variables : You can use it ( When defining variables, initialize )
Define variable format : type Variable name = initialization ;
( When defining variables : Type determines how much memory a variable needs to open up )
To assist in understanding : The data type is very similar to making a moon cake mold

If we want to make one Mooncake , We will certainly use Small mold To make , Can't use Big mold De application , The reason is that small molds can be made No wasted space , Large molds will cause a waste of space .
The same is true for data types If we want to store A character We will choose to use char Type to store , I don't want to use int type , Because a character only takes up one byte , just char Type also only opens up one byte , and int Type development 4 Bytes will cause a waste of space
Let's take over and learn about it C The size of the built-in inner class that is common in languages :

Why? long Follow int It takes up as much space ?
- because C++ The standard only stipulates long >= int
2. Naming rules for variables
- from Letter 、 Numbers 、 Underline form , But it can't start with a number . for example :max 、_max
- See the name and know the meaning . for example :max ( At first glance, you can see the maximum value represented by )、min( At first glance, we can see the minimum value represented )
- The name should concise , It's not easy to be too long . for example :MaxVal Just like MaxValueUntilOverflow
- When the identifier is composed of multiple words , Of each word The first letter is capitalized , The rest are in lowercase ( The name of the great hump ). for example :int CurrentVal
- In the program Not easy to Only by Case sensitive similar identifier . for example :int x = 0 、int X = 0 . Be careful :1( Numbers 1) and l ( Lowercase letters L), 0 ( Numbers 0) and o ( Lowercase letters O) The distinction between
- Function names are not easy to match variable names The same name .
- So Macro definition 、 Enumeration constants 、 A read-only variable Use both Name with capital letters , Underline splits words . for example :#define INT_MAX 100 、const int MAX_LENGTH = 100
- As far as possible with n 、i 、j Use as a loop variable
- Remember to initialize while defining variables , The compiler does not necessarily empty this memory when defining variables , Its value may be invalid
3. The most wronged keyword ——sizeof
Why is it the most wronged keyword ?
sizeof It's a keyword, not a function , But some people still think it is a function . Because most of us use it with parentheses for example :sizeof(int), The same is true of functions .
Let's clear up the grievances for this keyword , Prove that it is not a function !
We use the compiler to prove that it is a keyword, not a function :

From where we can see that it is not a function but a keyword ? Why the third printf ( ) It's wrong ?
- We can start from the fourth printf( ) See sizeof It's not a function , Because the function name should be followed by parentheses , and sizeof There is no parenthesis or error . Function calls are stacked ,sizeof Can't stack , Also explain sizeof It's not a function .
- One keyword cannot directly find the size of another keyword , So the third printf ( ) Will report a mistake .

Will the third printf( ) After shielding , The program can run normally .
notes :sizeof When calculating the space occupied by variables , Brackets can be omitted , The calculation type size cannot be omitted .
4、signed and unsigned
- signed: A signed
- unsigned: Unsigned
primary 、 back 、 repair
The creation of a variable is to open up space in memory , The size of space is determined according to different types
So how is integer data stored in the opened memory ?
① Signed number
int a = 10;
int b = -10;Signed numbers can be divided into positive numbers and negative numbers
The integer stored in the computer must be a complement , From this we have led to the original 、 back 、 The concept of complement
- Original code : Convert a number directly to binary
- Inverse code : The sign bits remain the same , Other bits are reversed
- Complement code : Inverse code +1
The original code of a positive number 、 Inverse code 、 The complement is the same
The original code of negative number 、 Inverse code 、 The complement is different , So the original code of negative numbers 、 Inverse code 、 Complements need to be converted to each other
Any data in the computer , Must be converted to binary , Why is that ?
answer : Because computers only know binary
Why must an integer stored in a computer be a complement ?
answer : In computer system , All values are represented and stored by complements . The reason lies in , Use complement , Symbol bits and value fields can be treated in a unified way ; Same as when , Addition and subtraction can also be handled in a unified way (CPU Only adders ). Besides , Complement code and original code are converted to each other , Its operation process is the same , No Additional hardware circuits are required
Original code 、 Inverse code 、 There are three representations of complement Sign bit and Value bits Two parts
- Symbol bit 0: It means a positive number
- Symbol bit 1: A negative number
example 1:signed int a = 10
First convert the literal value to Complement code , Then put the complement into the variable a in
Original code :00000000 00000000 00000000 00001010
because 10 Is a positive number , so Original code 、 Inverse code identical
Inverse code :00000000 00000000 00000000 00001010
Complement code :00000000 00000000 00000000 00001010
example 2:signed int a = -10
First convert the literal value to Complement code , Then put the complement into the variable a in
Original code : 10000000 00000000 00000000 00001010
because -10 It's a negative number , so Original code 、 Inverse code inequality
Inverse code : 11111111 11111111 11111111 11110101
Complement code : 11111111 11111111 11111111 11110110
notes : stay vs If the basic type in does not include signed and unsigned, The default is signed , But not necessarily in other compilers ( Most of them are signed )
Complement to original
Method 1 : Complement code -1 = Inverse code Inverse code inversion = Original code
for example :
Complement code :11111111 11111111 11111111 11101100
Inverse code :11111111 11111111 11111111 11101011
Original code :10000000 00000000 00000000 00010100
Method 2 : The complement symbol bit remains unchanged, and other bits are reversed by bit , then +1= Original code
benefits : You can use a hardware circuit , Complete the conversion
for example :
Complement code :11111111 11111111 11111111 11101100
10000000 00000000 00000000 00010011
Original code : 10000000 00000000 00000000 00010100
② An unsigned number
There is no sign bit , That means Original code = Inverse code = Complement code , When we take an unsigned integer variable You can take it directly
for example :unsigned int a = -10
Original code : 10000000 00000000 00000000 00001010
Inverse code : 11111111 11111111 11111111 11110101
Complement code : 11111111 11111111 11111111 11110110
hold -10 The complement of is stored in Unsigned integer a in , In the reading a The value in a variable is unsigned by default Directly convert the value stored in it into decimal system for printing .

Deeply understand the storage and retrieval of variable content
- save : Literal data must first be converted into complement , Put it in the space . therefore , The so-called sign bit , It depends entirely on whether the data itself carries +- Number . And whether the variable is signed irrelevant !
- take : To get data, you must first look at the type of the variable itself , Then decide whether to look at the highest sign bit . If you don't need to , Direct binary to decimal . If you need want , Then it needs to be converted to the original code , Then you can identify .( Of course , Where is the highest sign bit , Also specify the size end )
Large and small end

We can find from the above figure that the order of assigning values to variables is hexadecimal 12345678, But the order of hexadecimals displayed in memory is reversed , From this we can introduce a concept Large and small end

- Every byte has an address , All addresses are different , Then there must be size
- Addresses can be high and low ( In bytes )
int a = 0x12345678
- Data should also be divided into several blocks in bytes
- Data is in bytes , There are also high weights and low weights

The size end is the question of whether to place the high weight value at the high address or the low address ?
But no matter how you put it , As long as you use the same conditions to get
Big end ( Storage ) Pattern : The low bit of data is stored in the high address of memory , And the high end of the data , Low address saved in memory in

The small end ( Storage ) Pattern , The low bit of data is stored in the low address of memory , And the high end of the data ,, Stored in memory Address

Finally, I wish you all a healthy Dragon Boat Festival !

边栏推荐
- Warmly celebrate that yeyanxiu, senior consultant of Longzhi, won the title of "atlassian Certified Expert"
- Teach you to write word formula
- Distributed framework ray - detailed introduction to starting ray and connecting clusters
- FPGA面试题目笔记(二)——同步异步D触发器、静动态时序分析、分频设计、Retiming
- Sqli-libs range 23-24 filtration and secondary injection practice
- Shandong University machine learning experiment 5 SVM
- Data quality: the core of data governance
- Thymeleafengine template engine
- The meaning in the status column displayed by PS aux command
- What is a planning BOM?
猜你喜欢

SQLI_ LIBS range construction and 1-10get injection practice

Global case | how an airline with a history of 100 years can expand and transform to promote innovation in the aviation industry

Transfer Learning

FPGA设计中提高工作频率及降低功耗题目合集

FPGA设计——乒乓操作实现与modelsim仿真

C语言大战“扫雷”

Servlet

Servlet

我们真的需要会议耳机吗?

Error reporting injection of SQL injection
随机推荐
我们真的需要会议耳机吗?
Chapter 2 of machine learning [series] logistic regression model
Super explanation
Deployment of Flink
The difference between call and apply and bind
Login and registration based on servlet, JSP and MySQL
On the social moral and ethical issues behind short videos (personal point of view, for reference only)
Sword finger offer 50: the first character that appears only once
FPGA面试题目笔记(二)——同步异步D触发器、静动态时序分析、分频设计、Retiming
Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset
Twitter data collection (content, fans, keywords, etc.)
Implementation of data access platform scheme (Youzu network)
Shandong University machine learning final 2021
Continuous update of ansible learning
Stock K-line drawing
數組部分方法
qmake 实现QT工程pro脚本转vs解决方案
URL in flask_ for
Simple understanding of XML and JSON
The artistic director and production designer of Disney's Mandalorian revealed the virtual scene production behind it