The article directories

    • 1. Detailed introduction of data types
    • 1. Basic built-in types of C
    • Types of C language:
    • 2. Meaning of type:
    • 3. Basic classification of types
    • The storage of integer types in memory
    • 1. How is the data stored in the allocated memory?
    • 2. Why is complement stored in memory?
    • 3. How to store a and B in the memory
    • 4. Introduction to the big and small ends
    • 5. Causes of the large and small end
    • 5. 6. Smart water bottle
    • Floating-point storage in memory
    • 1. The representation of floating point numbers
    • 2. Floating point type stores the form in memory
    • End of chapter!!

1. Detailed introduction of data types

1. Basic built-in types of C

(C language itself has a type, we can directly use)

Types of C language:

1. Built-in types



2. Custom types (constructed types)



2. Meaning of type:

1. The size of memory space to open with this type (size determines the scope of use)

An integer 1 is only 4 bytes, so to save memory, we use int instead of long.

2. How to view the perspective of memory space

So float is 4 bytes, so if you look at memory from a float point of view, you can only store floating-point numbers. Ints are also 4 bytes, but if you look at memory from an int point of view, you’re storing integers. It’s a matter of perspective.

Code description:

int main(a)
{
	int a = 10;
	float b = 10.0;
	return 0;
}
Copy the code

Int and float are both 4 bytes, and we put a 10 in both variables a and b; Observe memory space



&a



&b

Memory Spaces are stored differently, with 10 in int as an integer and 10 in float as a floating-point number, proving that different types of memory Spaces are viewed differently.

Now that we’re talking about types, let’s classify them

3. Basic classification of types

(1) Plastic surgery family

Why does Char belong to the plastic surgery family?

Char is a character type, which is hard to classify, and characters are stored in memory as Ascll code values. Ascll code values are integers, and characters are also stored as integers, so they belong to the plastic family

Unsigned how does signed describe signed without sign?

Take the char type as an example:

Signed (signed)

A signed number with the highest sign bit. (0 – + 1—- +)

Unsigned

An unsigned number with no sign bits

11111111 corresponds to 255

(2) Family of floating point types

(3) Construct type (custom type)

(4) Pointer type

(5) Empty type

Often used for functions, void is used for functions that do not require a return value, and where a return type is used, it can be used for the parameters of the function.

The next section focuses on the memory storage of integers and floating-point types

The storage of integer types in memory

A variable is created to create space in memory, the size of which is determined by the type.

1. How is the data stored in the allocated memory?

Such as:

int a = 20;
int b = - 10;
Copy the code

We all know that A allocates 4 bytes, so how does A store it?

Here we need to understand the following concepts:





Through debugging, we know the contents of a and B stored in memory, so how do A and B convert such data?

The binary conversion of A and B is carried out. The complement codes are stored in memory and the complement codes of A and B are calculated



Ok, so when we get the complement, the memory is displaying hexadecimal numbers, and we convert the complement to hexadecimal,

Using the binary to hexadecimal rule, every four bits are represented by a hexadecimal number



So we come to a conclusion: for orthopedics, the data store memory actually holds the complement.

\

2. Why is complement stored in memory?



The CPU can only add

Let’s use a code to explain why complement is in memory. Right

int main(a)
{

	int a = 1;
	int b = - 1;
	int c = 1 - 1;
    return 0; }Copy the code

How can C calculate the result when CPU can only add

00000000 00000000 00000000 00000001 —-1 binary source code

10000000 00000000 00000000 00000001 —- -1 binary source code

If you just add them in the original code

So we’re going to get minus 2

Does 1-1=-2? Of course it turned out wrong!!

But convert the complement form

01111111 11111111 11111111 11111111 – The complement of 1

11111111 11111111 11111111 11111111 — The complement of -1

Result of addition

10000000 0000000 0000000 0000000 (C’s complement)

The integer can store only 32 bits. The 1 before the integer is omitted

C is an integer that can only hold 32 bits, so the complement is all zeros

C is 0; Correct result!!

By using this 1-1 counter example we can see why memory stores integer binary complement.

Back to the front \

3. How to store a and B in the memory

We can see that a and B store their complement, but not in the right order. Why is that? \

4. Introduction to the big and small ends



5. Causes of the large and small end

Why are there big and small ends?



Knowing the concept of size, we should know that our machines use small-endian storage, which is why we store backwards.

Take this opportunity to learn something about both the big and the small

5. 6. Smart water bottle

Please briefly describe the concepts of big-endian and small-endian, and design a small program to determine the current machine’s byte order



#include <stdio.h>
int check_sys(a)
{
 int i = 1;
 return(* (char *)&i);
}
int main(a)
{
 int ret = check_sys();
 if(ret == 1)
 {
 printf("Little end \ n");
 }
 else
 {
 printf("The big end \ n";)
 }
 return 0;
 }
Copy the code

Due to space constraints, we will cover the practice of large and small integer memory storage in the next blog post. Blog entry: C language advanced (two) – integer storage practice

Ok, so now that we know how integers are stored in memory, how are floating-point data stored in memory? \

Floating-point storage in memory

\

1. The representation of floating point numbers

\





For example:

5.0 in decimal, written in binary is 101.0, which is equivalent to 1.01 * 2 ^ 2. According to the expression of v above, it can be concluded that

S = 0;

M = 1.01;

E = 2;

\

2. Floating point type stores the form in memory

\



There are also the following specific rules for the significant number M stored in memory:

1.0 <= M < 2.0, M can be written as 1. XXXXX, where XXXX is the decimal part



For example, 5. 0 is 1.01 * 2 ^ 2. The significant digit is 1.01.





M out of

Of course, when you take a significant digit out of memory, remember to take out the decimal place of the significant digit, and automatically fill in the decimal place with 1.







With the exponent E, the situation is more complicated





E is stored in memory











Take the 8-bit E for example

Take 0.5 in decimal

0.5 is converted to binary to 0.1

0.1

1.0 times 2 to the minus 1.

So E is negative 1, negative, but E is an unsigned integer.

So when we store E in memory, we need a middle number, and for 8-bit E, the middle number is 127, so we actually store E in memory = -1 + 127 = 126.

\

E gets out of memory



All E is not 0 or all E is not 1, which is the normal situation. You only need to change e-127 to get the real E.

When E is all 0, if E + 127 = 0, then true E = -127, a number to the -127 power is approximately 0, so what we get is a number that is infinitely close to 0 (almost non-existent), so IEE754 states, E = 1-127 or E = 1-1023 is the true value, the significant digit does not add the first 1, directly +/ -0. XXXX * 2^(-126).

E is all 1s, resulting in +/- 1.xxxx * 2 ^ (128), a number representing plus or minus infinity. So much for storing integer and floating point types in memory. I hope you can practice a lot and be familiar with the rules of storing these two types in memory. \

Thank you !!!!

End of chapter!!


\

C language Advanced (2) – Plastic storage exercise has been updated