当前位置:网站首页>Storage of C language integer in memory

Storage of C language integer in memory

2022-06-21 08:34:00 With special education

Data in memory


C What are the data types

 Insert picture description here







Explain it separately void type
 Insert picture description here








The meaning of data type

1. Use this type to exploit the size of memory space ( Size determines the range of use ).
2. How to look at the perspective of memory space .



Understand the basic knowledge of integer storage in memory

1. The data in memory is in the form of 2 Base store
2. There are three ways to express integers in a computer , The original code 、 Inverse and complement
But in essence, integers are stored as complements
3. The first bit represents the sign bit , The sign bit uses 0 Express “ just ”, use 1 Express “ negative ”

Positive integer : Original code 、 Inverse code 、 The complement is the same

Negtive integer : Original code 、 Inverse code 、 Complements are not the same

Original code : The original code is written directly according to the binary of the number

Inverse code : The sign bits remain the same , The reverse of other bits is the reverse code

Complement code : Inverse code +1 It's complement


The order of data storage in memory

Large byte storage : The lower bits of the data are stored at the higher address , The high bit of data is stored at the low address .

Small end byte storage : The lower bits of the data are stored at the lower address , The high bit of data is stored at the high address .




Example :1 Store on the large and small sides of memory

int a=1  0x00 00 00 01 -16 Base number 

 If it is    The small end    Then it is stored in memory in this way  01 00 00 00         Big end  00 00 00 01

 Be careful :VS2019 Using small end storage 



Design a program to determine whether the current compiler is large-end storage or small-end storage

#include <stdio.h>
int check_sys()
{
    
	 int i = 1;  
	 return (*(char *)&i);
}

int main()
{
    
	 int ret = check_sys();
	 if(ret == 1)
	 {
    
	 printf(" The small end \n");
 	}
 	else
	{
    
 		printf(" Big end \n");
 	}
 	return 0; 
}


principle : Actually used char Step size for type access , because char Access only one byte at a time , We just need to judge the value of the first byte If the value is 1 The explanation is small end , by 0 Description is big end .

 Please add a picture description


Program exercises

#include <stdio.h>
int main()
{
    
    char a= -1;
    signed char b=-1;
    unsigned char c=-1;
    printf("a=%d,b=%d,c=%d",a,b,c);
    return 0; 
}

char a= -1 ( Save complement )
Original code :10000000000000000000000000000001
Inverse code :1111111111111111111111111111111111110
Complement code :1111111111111111111111111111111111111


And because it's char Type can only be saved 8 individual bit So the truncated storage is 11111111


To press %d The printing should be improved and the original code should be printed because char It is a signed number, and the lifting complement is the original sign bit
1111111111111111111111111111111111111 - Complement code
1111111111111111111111111111111111110 - Inverse code
10000000000000000000000000000001 - Original code

So the answer is -1 ,char b Is the same

unsigned char c=-1
What you save is 11111111
To press %d The printing should be improved and the original code should be printed because char It is an unsigned number that is raised and complemented by 0
0000000000000000000000011111111 - Complement code - Complement code - Original code The positive integer is the same as the original inverse complement
The answer is 255


#include <stdio.h>
int main()
{
    
    char a = -128;
    printf("%u\n",a);
    return 0; 
}

a=-128
10000000000000000000000010000000 - Original code
111111111111111111111111111101111111 - Inverse code
11111111111111111111111111111000000 - Complement code


the reason being that char a Deposit is 10000000
%u Print unsigned print is a complement
Plastic improvement is
11111111111111111111111110000000 - Complement code ( At this point, the highest bit is not the sign bit )
So the print is 4294967168


#include <stdio.h>
int main()
{
    
    char a = 128;
    printf("%u\n",a);
    return 0; 
}

a=128
00000000000000000000000010000000 - Original code
011111111111111111111111111101111111 - Inverse code
01111111111111111111111111111000000 - Complement code
char a=10000000
Plastic surgery improves
11111111111111111111111111110000000 - Complement code
Because it is printed as an unsigned number, the first sign is not the same as the original reverse complement of the sign bit
What is printed is still 4294967168


int i= -20;
unsigned  int  j = 10;
printf("%d\n", i+j); 
// Operate in the form of complement , Finally, it is formatted as a signed integer 

i=-20
111111111111111111111111111111101100 - Complement code
j=10
00000000000000000000000000001010 - The original is the same as the reverse
Addition is the addition of complements
i+j
111111111111111111111111111111110110 - Complement code
Press %d The original code is required for printing
111111111111111111111111111111110101 - Inverse code
10000000000000000000000000001010 - Original code
So the print is -10


 Insert picture description here




Here I borrow the picture of big brother Lin slowly
 Insert picture description here

 Insert picture description here
answer 128+127=255


7.
#include <stdio.h>
unsigned char i = 0;
int main()
{
    
    for(i = 0;i<=255;i++)
   {
    
        printf("hello world\n");
   }
    return 0; 
}

Because unsigned numbers are always less than or equal to 255 Dead cycle


Please forgive me for your poor writing !

原网站

版权声明
本文为[With special education]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221453226502.html