Detailed Explanation of C Language Keywords (3) Data Type and Sizeof Keyword

I. Introduction

Hello everyone, welcome to the C language in-depth analysis column - the third part of the C language keyword detailed explanation. In this article, we will introduce the data types in the C language, and thus lead to another important keyword in the C language. — sizeof .

Second, the data type

1. What are the data types

Data types in C language include primitive types (built-in types), constructed types (custom types), pointer types, and null types
insert image description here

2. Why do we need data types?

Why have built-in types

We encounter various scenarios in our daily life, and different scenarios require different data to represent, such as the number of people we have dinner, the temperature of the weather, the altitude and other things are usually described by integers, like the height of a person , the frequency of broadcasting, and the price of goods are usually expressed in decimals. For example, our license plate number, the name of the building, and the size of clothing need to be expressed in letters; C language, as the first high-level programming language, in order to be able to To accurately describe various scenarios in our lives, there are built-in types such as integers, floating-point types, and character types.

Why have custom types

Let's take the array type and structure type in the custom type as an example:
Array type: We will encounter many sets of the same type in our life, such as the student number of a school student, and the student number of each student is an integer, so in order to To represent the student numbers of all students, it is necessary to define thousands of integers. Obviously, that is too troublesome, so an array type is generated. The type of each element in an array is the same. We are defining a school student's When you are a student, you only need to define an array with a size of several thousand elements, instead of slowly defining a thousand integer
structure types: the objects we want to describe in our lives are often collections of complex data types. For example, a person has name, gender, height, weight, age, etc. These data types are all different, so in order to systematically describe the attributes of a person, a structure type is generated, which converts a person to different types. All data is centralized into a new type, which makes object description and use more convenient.

3. How to treat data types

From the previous blog, we know that the essence of defining variables is to open up a space in memory to store data, and today we know that different variables need to be defined as different types. Combining the two, we will not It's hard to come out: the type determines the size of the space opened up by the variable.
At this time, there are two questions. First, why should we open up space according to the type? We can directly open up a space and use the memory as a whole, right? The answer is: not good.
There are two main reasons:
1. At any time, your computer is not only running the program you are currently using, there are many other programs running at the same time, if the entire block is allocated to you currently running program, other programs crash.
2. Even if the entire block of memory is allocated to you, you cannot guarantee that the memory block will be used up at any time, which will lead to a waste of memory.
Second, we use part of the memory, how much is used by what? The answer is: it is determined by your scenario, your calculation scenario, and what type of variables you use for calculation. The type you use determines how many bytes of space you open up. This is why the C language has so many data types, just to meet different computing scenarios.
Finally, how much space do different data types open up in memory? This needs to be calculated using our keyword – sizeof.

Three, sizeof - Calculate the size of the space opened up by different types of variables

1. The size of the space opened up by built-in types

`#include<stdio.h>
int main()
{
    
    
	printf("%d\n", sizeof(char));       //1
	printf("%d\n", sizeof(short));      //2
	printf("%d\n", sizeof(int));        //4
	printf("%d\n", sizeof(long));       //4
	printf("%d\n", sizeof(long long));  //8
	printf("%d\n", sizeof(float));      //4
	printf("%d\n", sizeof(double));     //8
}`

insert image description here

2. The size of the space opened up by the custom type

array size

#include<stdio.h>
int main()
{
    
    
	int arr1[10] = {
    
     0 };       //40
	char arr2[10] = {
    
     0 };      //10
	long int arr3[10] = {
    
     0 };  //40
	long long arr4[10] = {
    
     0 }; //80
	float arr5[10] = {
    
     0 };     //40
	double arr6[10] = {
    
     0 };    //80
	printf("%d\n", sizeof(arr1));
	printf("%d\n", sizeof(arr2));
	printf("%d\n", sizeof(arr3));
	printf("%d\n", sizeof(arr4));
	printf("%d\n", sizeof(arr5));
	printf("%d\n", sizeof(arr6));
	return 0;
}

insert image description here

From the above results we can easily draw: the size of the array = the type of the array elements multiplied by the number of elements

Other custom type sizes

#include<stdio.h>
struct Test1{
    
    
	int a;
	char b;
	float c;
	double d;
};
union Test2{
    
    
	int m;
	char n;
};
enum Test3 {
    
    
	monday,
	tuesday,
	wednesday,
	thursday,
	frifay
};
int main()
{
    
    
	struct Test1 test1 = {
    
     0 };
	union Test2 test2 = {
    
     0 };
	enum Test3 test3;
	printf("%d\n", sizeof(test1));   //24
	printf("%d\n", sizeof(test2));   //4
	printf("%d\n", sizeof(test3));   //4
}

Presumably the above results are different from the results in the minds of some small partners. Indeed, the size of the custom types such as structures, unions, and enumerations and the size of the array are not the same. The specific methods involve memory alignment and size. Terminal, memory allocation and other related knowledge, these knowledge are more complicated, I will explain it in the custom type detailed module, and now you don't need to go into it.

3. The size of the space opened up by the pointer type

insert image description hereinsert image description here

As you can see, no matter what the type of the pointer is (integer, character, floating-point, array), the size of the pointer is always four bytes or eight bytes (the first picture X86 represents 32 bits platform, the result is 4, the second picture X64 represents the 64-bit platform, the result is 8), so the conclusion is: the pointer is 4 bytes under the 32-bit platform, and 8 bytes under the 64-bit platform. (As for why this is the case, this involves knowledge of memory addressing, address lines, etc. I will explain this part in detail on the pointer. Now you only need to remember this conclusion)
Note: The second picture has a warning It's because my computer is a 32-bit platform, and forcing it to 64-bit will cause a size mismatch.

4. The size of the space opened up by the empty type

insert image description here

We can see that although the compiler reports an error here, it still prints the size of void: 0 bytes
Note: The size of the void type is 0 bytes, which is only the result of running under the visual studio compiler , however, this result may be different in different programming environments, for example, in the Linux environment, the size of the void type is 1, (due to the level limitation, it cannot be demonstrated here for the time being); The fundamental reason for the difference is that different compilation environments have different degrees of support for the C language.

4. Further understanding of sizeof

1. Why is sizeof not a function

insert image description hereinsert image description here

From the above, we can see that we can use sizeof(a) and sizeof(int) to find the size of an integer. This method is also familiar to everyone, but we found that
sizeof a can also be used to find the size of a directly, and No parentheses are needed, so sizeof is a keyword (operator) but not a function, because function parameters need to be () to be used properly.
Note: sizeof int reports an error because sizeof and int are both keywords, and you cannot use one keyword to find the size of another keyword

2. Other uses of sizeof

insert image description here

Here we define an integer variable a and a pointer variable p, as well as an array arr. We can see that the size of a is 4 and the size of arr is 40. We all understand these,
then the remaining sizeof § , sizeof(&arr), What does sizeof(arr) / sizeof(arr[0]) mean? The following is an explanation for everyone (involving pointer-related knowledge)

p is a pointer variable, which stores the address of a, arr array name indicates the address (memory) of the first element of the arr array, &arr indicates the address of the entire array, which is equivalent to an array pointer, so sizeof§ and sizeof(&arr) Both are the size of the pointer sought, and as we know above, the pointer is 4 bytes under the 32-bit platform, so the result here is 4.
Finally, sizeof(arr) finds the size of the entire array, and sizeof(arr[0]) finds the size of the first element, so dividing the two results in 10 elements of the array.
Note: sizeof(arr[0]) is used here to find the size of an array element, instead of arr[1], arr[2] because we don't know how many elements the array has, so maybe arr[1], arr [2] does not exist at all, but as long as the array is defined, then arr[0] must exist, that is, it is done for safety.

More keywords are in the blog link below.
Detailed explanation of C language keywords (1) Auto, register keyword
detailed explanation of C language keywords (2) Take you to a comprehensive understanding of static

         码字不易,求个三连

insert image description here

Guess you like

Origin blog.csdn.net/m0_62391199/article/details/123584280