In-depth analysis of the storage of integer data in memory

Foreword: In the last blog, we understood how integers are stored in memory, and briefly introduced integer promotion. Now, we will continue to learn more about the storage of integer data in memory.

char type

integer promotion

Please see the following code:

//  大家觉得打印结果是什么呢,或者这样写能打印吗?
	char a = 10;
	printf("a=%d", a);

After running, the console window displays the result as shown in the figure:
insert image description here
Seeing this, I believe some friends will have doubts: Isn’t char specially used to store characters? Why does it output an integer value of 10?
The details are as follows:
1. Char type We can regard it as a special integer type, occupying 1 byte, that is, 8 bits.
2. No matter what the variable type is, we store an integer number in the memory, that is, store its 2’s complement code, and the displayed result follows its interpretation method (%d, %u, etc. ) to show
3. Integers are stored in the memory in the form of complement code, and the output is in the form of original code

	char a = 10;
//  00000000 00000000 00000000 00001010  原码
//  00000000 00000000 00000000 00001010	 反码
//  00000000 00000000 00000000 00001010  补码
//  char占8比特位,截断—— 00001010 (10的补码的后8位)
//  输出形式是%d,占32比特位,则需整型提升,补符号位,00001010即补0
//  00000000 00000000 00000000 00001010 —补后的反码 ——符号位为0,则是正数,原反补码相同
//  读原码为10,则输出结果是10

char type storage range

We know that the storage range of the int type is -2^31 ~ 2^31-1, and so on, so what is the storage range of the char type?
insert image description here
The above pictures are all complement codes

  char类型占据8个比特位,则最大正数的补码为01111111,即127,01111111再加上1则变为10000000
  10000000开头为符号位,符号位为1则代表了它是一个负数,即-128,往后数字以此类推,构成一个循环。

So the value range of the char type is -2^7 ~ 2^7-1(-128 ~ 127)

Unsigned char type storage range

Above we discussed the storage range of the char type, please try to find the storage range of the unsigned char type.

The specific calculation method is roughly the same as above. The char type occupies 8 bits, 01111111, which is 127, and 01111111 plus 1 becomes 10000000, but this
is an unsigned type, and the beginning is not a sign bit, so we directly read 10000000 as 128. Then the maximum number is 11111111, which is 255

So the value range of the unsigned char type is 0 ~ 2^8-1(0 ~ 255)
. After these two examples, similarshortlongYou can calculate the storage range of the integer type.

Various situations that may be encountered

Before doing the question, keep this in mind
No matter what the variable type is, we store an integer number in the memory, that is, store its 2’s complement, and the displayed result is based on its interpretation method (%d, %u, etc.) exhibit
Just talking but not practicing fake moves, please see examples of various situations below

Print char type in the form of %d, %u

	char a = -128;
	char b = -128;
	printf("%d\n", a);
	printf("%u\n", b);

insert image description here
Parse:

	char a = -128;
	10000000 00000000 00000000 10000000  -原码
	11111111 11111111 11111111 01111111  -反码
	11111111 11111111 11111111 10000000  -补码
	10000000 - 截断 ,补符号位1(整型提升)
	11111111 11111111 11111111 10000000  补后的补码
	11111111 11111111 11111111 01111111  反码
	10000000 00000000 00000000 10000000  原码 ——输出结果为-128
	
	char b = -128
	11111111 11111111 11111111 10000000 - -128的补码
	10000000 —  截断,补符号位1(整型提升)
	11111111 11111111 11111111 10000000   补后的补码  无符号数原反补码相同 ——输出结果为 4294967168

Print the added value of int type and unsigned int type in the form of %d

	int i = -20;
	unsigned int j = 10;
	printf("%d\n", i + j);

Output result: insert image description here
Parsing:

The int type can store -20; the unsigned int type can store the next 10. From the perspective of value alone, there is no change, so it can be added directly to get -10, and then printed as a signed number to get - 10.
Note: Interested friends can write out their original inverse complements as above and add them together.

Unsigned number as a loop judgment condition

#include<stdio.h>
#include<stdlib.h>
int main()
{
    
    
	unsigned int i;
	for (i = 5; i >= 0; i--)
	{
    
    
		printf("%u\n", i);
		Sleep(1000);//加入Sleep函数以观察到控制台显示的变化
	}
	return 0;
}

insert image description here
We found that the numbers printed after the result 0 are very large, and the loop does not stop. why?

0 becomes -1 after passing through i–, but i is the complement of an unsigned number
11111111 11111111 11111111 11111111 -1 .

The storage size of char type?

#include<stdio.h>
#include<string.h>
int main()
{
	
	char a[500];
	int i = 0,ret = 0;
	for (i = 0; i < 500; i++)
	{
		a[i] = i+1;
	}
	ret = strlen(a); //strlen是计算字符串长度的,遇到'\0'停止,而'\0'的ASCII码值为0.即遇到数组元素为0时停止循环
	printf("%d", ret);
}

insert image description here

	for (i = 0; i < 500; i++)
	{
		a[i] = i+1;   // 1,2,3,4,5…………127,-128,-127,-126…………-1,0  读到0停止循环
	}
	上面我们知道char类型取值范围为-128 ~ 127,所以127后为-128(详细请看文章开头)而非128
	-128,-127…………-1,0  读到0停止,所以输出结果就是255

Here is another example, try to do it yourself

	unsigned char i = 0;
	for (i = 0; i <= 255; i++)
	{
		printf("我爱中国\n");
	}

BB at the end of the article: Well, this is the end of the article. Due to the length of the article, the blogger may have made some mistakes when making it. Readers are welcome to remind, and the blogger will change it as soon as possible. If it is helpful to you, please give the blogger a like. Thanks!
insert image description here

Guess you like

Origin blog.csdn.net/qq_73390155/article/details/129280882