preface:
For beginners, there are two major difficulties in C language, one is the pointer class with complex changes, and the other is the related data storage memory distribution. This article focuses on the data storage related to C language
summary:
In C language, there are many types of data. Since it is the storage of data, we can't avoid talking about data types. Before introducing their types, we should first understand why C language classifies data by types?
Meaning of data type:
- The data uses the size of the space opened up by this type in the memory space, and the size also determines the scope of use
- We look at data from different perspectives through different types
Basic classification of data types
We mainly classify data types into: integer family, floating-point family, construction type, pointer type and null type. Next, let's talk about the specific types covered by the next category
Integer family
char | short | int | long |
---|---|---|---|
unsigned char | unsigned short | unsigned int | unsigned long |
signed char | signed short | signed int | signed long |
Floating point family
float | double |
---|
Construction type - custom type
Array type | Structure type (struct) | Enum (enum) | Consortium |
---|
ps: array type and custom type:
eg: int arr[10] - the data type is int [10],
int arr[5] - the data type is int [5]
Pointer type
int* pi | char* pc | float* pf | void* pv |
---|
Empty type
void indicates null type (no type) | It is usually applied to the return type, parameter and pointer type of a function |
---|
How data is stored in memory:
After we understand the basic classification of data types, we need to understand that data is stored in binary of 01 in the computer
1. There are three binary representations of data: original code, inverse code and complement code
Original code: write binary code directly according to the data value
Inverse code: reverse the original code
Complement: inverse code + 1 - for details, see - link -——
Storage of data:
Next, enter the focus of this chapter: data storage
- Data is stored in binary form in memory.
- Data is stored in memory in the form of complement. See details for reasons: - link -
Let's explain the storage of integer family in memory through several examples
Example 1:
int main() { char a = -1; //First of all, I think that what it stores in memory is a complement //Write the original code first //10000000000000000000000000000001 //Take the complement of inverse + 1 //11111111111111111111111111111111 //Because it is a char type, only 8 bits can be stored //11111111 //Integer promotion: if there is a sign bit, fill the sign bit in front of it //11111111111111111111111111111111111111111 -- complement //When the complement is printed in the form of% d, it depends on its original code -- print -- - 1 signed char b = -1; //Deposit 11111111 //Signed char is the same as a unsigned char c = -1; //Deposit 11111111 //Unsigned char //Integer promotion: 0 before unsigned bit //00000000000011111111 -- the sign bit is 0, is a positive number, and the complement is the same as the original code -- print -- 255 printf("a=%d,b=%d,c=%d", a, b, c);//Because the type to be output is% d, that is, int type, which involves integer lifting retrun 0; }
Supplement:
1. Is char type signed char or unsigned char?
2. The C language standard is not specified and depends on the compiler.
3.int is signed. Int is specified in the C language standard.
4.short is also a signed short.
Example 2
int main() { char a = -128; //100000000000000000000000010000000 //111111111111111111111111101111111 //111111111111111111111111111111111111110000000 - complement //10000000 - char stored in memory //Integer lifting according to the complement of the original char //111111111111111111111111110000000 //%u printing unsigned integers is to treat the binary number promoted by shaping as a positive number printf("%u", a);//429... 168 a large value return 0; }
//char type
//Value range of signed char: - 128 ~ 127
Example 3
int main() { int i = -20; //10000000000 10100 - Original //11111111111111111111111111111111111111101011 - reverse //111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 unsigned int j = 10; //0000000000000000 1010 - Original - reverse - Supplement // 00000000000000000000000000001010 -- -10 // 11111111111111111111111111111111111111111111111111111111111111111111111111111101100 - supplement -- 20 // 11111111111111111111111111111110110 -- obtained after adding printf("%u\n", i + j); return 0; }
Example 4
int main() { //Dead cycle unsigned int i = 0;//Here i is constant > = 0, because it is an unsigned number, and the minimum is 0 for (i = 9; i >= 0; i--) { printf("%u\n", i);//%u and% d determine the final value of i, } return 0; }
Example 5
int main() { char a[1000]; int i; for (i = 0; i < 1000; i++) { a[i] = -1 - i; } //-1 -2 -3 .....-127 -128 127 126 125......3 2 1 0 -1 -2 ...-127 -128 127... printf("%d", strlen(a));//Find \ 0 - > 0 (\ 0's ascii code value is 0) //The result is 225 return 0; }
Summary:
To judge different types of print results, you need to pay attention to the following aspects:
- From the perspective of data type, this data is char, int, double
- Note the type of output:% d,% u,% c... See - link -
- Is there an integer lift
- The starting point of judgment is to keep in mind that data is stored in memory in the form of complement
Author: @ luobotou kimi