Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of C language data types and sizeof keywords

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "C language data types and sizeof keyword case analysis". The editor shows you the operation process through the actual case, the operation method is simple, fast and practical. I hope this article "C language data type and sizeof keyword case analysis" can help you solve the problem.

I. Preface

This paper introduces the data types in C language, and leads to another important keyword sizeof in C language.

2. Data type 1, what are the data types

The data types of C language include basic types (built-in types), construction types (custom types), pointer types, and null types.

2. Why should there be a data type

Why is there a built-in type?

We encounter a variety of scenes in our daily life, and different scenes need different data to express. For example, the number of people we eat together, the temperature of the weather, the altitude and other things are usually described by integers, while people's height, broadcast frequency and commodity prices are usually expressed in decimals. For example, our license plate number, the naming 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 accurately describe a variety of scenes in our lives, there are plastic, floating-point, character and other built-in types.

Why should there be a custom type

Let's take the array types and structure types in custom types as examples:

Array type: we will encounter many sets of the same type in our life, such as the student number of a school student, and each student's student number is plastic, so in order to represent the student number of all students, it is necessary to define thousands of plastic operations. Obviously, that is too troublesome, so the array type is generated, and every element in an array is of the same type. When we define the student number of a school, we are defining the student number of a school. You only need to define an array of thousands of elements, instead of slowly defining a thousand shaping.

Structure type: the object we want to describe in our life is often a collection of complex data types. for example, a person has a name, sex, height, weight, age, etc., these data types are all different, so in order to systematically describe a person's attributes, a structure type is generated, which centralizes different types of data of a person into a new type. It makes the description and use of objects more convenient.

3. How to treat the data type

From the previous blog, we know that the essence of defining variables is to open up a space in memory to store data, but today we know that different variables need to be defined as different types. It is not difficult to conclude that the type determines the size of the variable opening space.

At this time, there are two questions: first, why should we open up space according to the type? is it not good for us to directly open up a piece of space and use the memory as a whole? The answer is: no.

There are two main reasons:

1. At any time, your computer is not just running the program you are currently using, but many other programs are also running at the same time. If you assign the whole block to the program you are currently running, the other programs will crash.

2. Even if the whole block of memory is allocated to you, you cannot guarantee that it 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 determined by what? The answer is: it is your scenario that determines what type of variables you use to calculate. The type you use determines how many bytes of space you open up. This is why the C language has so many data types in order 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.

3. Sizeof-calculate the size of the open space of different types of variables 1. The size of the space opened by the built-in type` # includeint 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} `

2. The size of the space opened by custom types

Array size

# includeint 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;}

From the above results, we can easily get the size of the array = the type of the array elements multiplied by the number of elements.

Size of other custom types

# includestruct 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 those in the minds of some partners, indeed, structures, unions, enumerations of these custom types of size and array size are not the same, the specific solution involves memory alignment, size end, memory allocation and other related knowledge, this knowledge is more complex, I will be put in the custom type detail module to explain for you, now we do not need to delve into.

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

As you can see, regardless of the type of pointer above (shaping, character, floating-point, array), the size of the pointer is always four or eight bytes (the first figure X86 represents a 32-bit platform, the result is 4). The second figure X64 represents a 64-bit platform with a result of 8), so the conclusion is that the pointer is 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform. (as for why this is the case, it involves knowledge about memory addressing, address lines and so on. I will explain this part in detail on the pointer. Now you just need to remember this conclusion.)

Note: the second picture has a warning because my computer is on a 32-bit platform, and there will be a size mismatch when forced to convert to 64-bit.

4. The amount of space opened by the empty type

We can see that although the compiler reported an error here, it still prints out the size of the void: 0 bytes

Note: the size of the void type is 0 bytes, which is only the result of running under the compiler visual studio. However, this result may vary in different compilation environments, such as in the Linux environment, the size of the void type is 1 (due to horizontal limitations, it can not be demonstrated here for the time being); the fundamental reason for the difference between the two is that different compilation environments have different degrees of support for the C language.

4. Further understanding of sizeof 1. Why sizeof is not a function

As we can see from above, we can use sizeof (a) and sizeof (int) to find the size of a plastic surgery, which is also familiar to everyone, but we find that we can directly use the

Sizeof a can also figure out the size of a without parentheses, so sizeof is a keyword (operator) but not a function, because function arguments need to be used with ().

Note: sizeof int reports errors because both sizeof and int are keywords, and you cannot use one keyword to find the size of another keyword.

2. Other uses of sizeof

Here we define an integer variable an and a pointer variable p, as well as an array arr. We can see that the size of an is 4 and the size of the pointer is 40, which we all understand.

So what does the rest of sizeof §, sizeof (& arr), sizeof (arr) / sizeof (arr [0]) mean? The following is an explanation (involving pointer-related knowledge)

P is a pointer variable, which stores the address of a, the arr array name represents the address of the first element of the arr array (memory), and & arr represents the address of the entire array, which is equivalent to an array pointer, so sizeof §and sizeof (& arr) are both the size of the pointer, and above we know that the pointer on the 32-bit platform is 4 bytes, so the result here is 4.

Finally, sizeof (arr) calculates the size of the entire array, and sizeof (arr [0]) calculates the size of the first element, so dividing the two gets the number of elements of the array 10.

Note: here we use sizeof (arr [0]) to find the size of an array element, rather than arr [1] and arr [2] because we don't know how many elements there are in the array, so maybe arr [1] and arr [2] don't exist at all, but as long as the array is defined, then arr [0] must exist, that is to say, it is for security.

This is the end of the introduction to "C language data types and sizeof keyword example Analysis". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report