C language data storage-on

Data storage in C language-on

Due to the large content, it is divided into two parts, the storage of data-I will rush out as soon as possible. It doesn't matter if you like it or not. I just hope that readers who see it can spend your precious few minutes looking at the articles I have written.

First introduce what are the data types

  1. Plastic surgery
char//其在内存中以ASCII码的形式存储,ASCII码为整数,所以归为整形类
	signed char//有符号
	unsigned char//无符号
short
	signed short
	unsigned short
int 
	signed int
	unsigned int
long
	signed long
	unsigned long
long long
	signed long long
	unsigned long long
//仅写出char/short/int/long/long long时通常都会被理解为有符号型
  1. Floating point class
float
double
long double
  1. Constructed type (custom type)
数组类型-例如arr[10]={
    
    0}
结构体类型-struct
枚举类型-enum
联合类型-union
  1. Pointer type
int *pi;
char *pc;
short *ps;
float *pf;
void *pv;//空类型指针
  1. Empty type
void表示空类型/无类型
通常应用于函数的返回类型、函数的参数、指针类型。

The significance of creating these types is:

  1. The use of different types determines the size of the open memory space (the size of the space determines the scope of application)
  2. The type determines the perspective of looking at the memory space.
    Next, let’s first explain the storage form of the plastic in the memory.
    If you want to create a variable, you need to open up space in the memory. The size of the opened space is related to the type. Then let’s take a look at the variable. How is it stored in memory.
比如
int a=20;
int b=-10;
我们已经知道a,b会占内存四字节的空间,那它们是如何存储的呢?

Let's first understand the concepts of original code, inverse code, and complement code.
There are three ways to represent the signed number in the computer, namely the original code, the inverse code and the complement code
. There are two parts of the sign bit and the value bit. The sign bit uses 0 to indicate "positive" and 1 to indicate " "Negative", and the three representations of numerical bits are different.

  • Original code: directly translate the binary into binary in the form of positive and negative numbers
  • Inverted code: The sign bit of the original code remains unchanged, and the other bits can be inverted sequentially.
  • Complement code: add one to the inverse code. The
    above is the way to find the original inverse complement of a negative number, and the original inverse complement of a positive number is the same.

For shaping: the data stored in the memory is actually the complement

The reason is that

In computer systems, values ​​are always represented and stored in complements. The reason is that the sign bit and the value field can be processed uniformly by using the complement code; at the same time, addition and subtraction can also be processed uniformly (CPU only has an adder). In addition, the complement code and the original code are converted mutually, and the operation process is the same. Additional hardware circuit.

Show an example of positive and negative numbers
Insert picture description here

Insert picture description here
Then another question arises from this. Why is the storage order of the complement in the above figure different from that in the memory?

Next, in order to explore this issue, let's take another chestnut. Suppose how to store the number 0x 11 22 33 44 now?
Insert picture description here
Big-endian byte order storage : store the content of the low byte of a data (the first type in the above figure) at the high address; store the content of the high byte at the low address.
Little-endian byte order storage : store the content of the low byte of a data (the second type in the above figure) at the low address; store the content of the high byte at the high address.

This is because in computer systems, we use bytes as the unit, and each address unit corresponds to a byte, and a byte is 8 bits. But in the C language, in addition to the 8-bit char, there are 16-bit short type, 32-bit long type (depending on the specific compiler), and for processors with more than 8-bit bits, such as 16-bit or 32-bit Because the register width is greater than one byte, there must be a problem of how to arrange multiple bytes. This led to the big-endian storage model and the little-endian storage model.
For example, a 16-bit short type x, the address in the memory is 0x0010, and the value of x is 0x1122, then 0x11 is the high byte, and 0x22 is the low byte. For big-endian mode, put 0x11 in the low address, that is, 0x0010, and 0x22 in the high address, that is, 0x0011. Little-endian mode is just the opposite. Our commonly used X86 structure is little-endian mode, while KEIL C51 is big-endian mode. Many ARMs and DSPs are in little-endian mode. Some ARM processors can also choose big-endian mode or little-endian mode by hardware.

So here is a small programming question: Please design a program to determine whether the storage mode of the current platform is big-endian or little-endian?

#include <stdio.h>
int check()
{
    
    
	int a = 1;
	return *(char*)&a;//先将int类型的指针强制转化为char,再解引用传回a中第一个字节数据
}
//上面拆开写就是这种
//int check()
//{
    
    
//	int a = 1;
//	char *p = (char*)&a;
//	return *p;
//}
int main()
{
    
    
	int a = 1;
	if (check() == 1)//如果返回的是1,就证明低位字节在低地址上
	{
    
    
		printf("小端\n");
	}
	else//如果返回的是0,就证明低位字节在高地址上
	{
    
    
		printf("大端\n");
	}
	return 0;
}

The storage of C language data-this is the end.
I hope that the big guys who have seen it will give a lot of advice.

Guess you like

Origin blog.csdn.net/weixin_53451597/article/details/114286073