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

What are the variable types in C language and how to use them?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the variable types in C language and how to use them". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the variable types in C language and how to use them?"

one. The type of the variable tells the compiler what to do with the variable's data.

Although c is a strongly typed language, different types of variables can be assigned by type conversion, and even pointer variables can be converted to int types and char types. In essence, the variable type simply tells the compiler what to do with the variable, so different variables can be assigned by displaying type conversions. Understanding this is very important for us to understand the transformation of pointers. For example

Int a = 10; int * * ptr = & a; int b = (int) (* ptr); / / * ptr is a pointer, but by transforming to int, we can assign it to b.

If you don't understand the nature of variable types, you will think that the third line of code is wrong. Why can it be assigned in this way? (* ptr) is a pointer, but the pointer also has a value. Its value is an address, which is actually an unsigned integer. So we change to int through type conversion, and the data of this variable in memory has not changed, but the parsing of it has changed. If it is a pointer type, then the data is parsed as a pointer; if it is an int type, then the data is parsed as an int. In fact, we can also assign it to the char type. Convert it to char, and its parsing is of type char. Only one byte of the data is parsed into type char, and a value is assigned to ch.

Char ch = (char) (* ptr); / / some compilers will report errors, which can be corrected by changing them to (char) (int) (* ptr).

two. The type of the variable tells the compiler how much memory space to allocate.

When defining a variable, the variable type tells the compiler how much memory space to allocate to store the variable.

Char ch; / / 1B int I; / / 4B long l; / / 4B float f; / / 4B double; / / 8B

By the way, how to understand multi-level pointers. For example, int * * ptr

When we encounter secondary pointers and tertiary pointers, we always don't understand what all levels of pointers mean. There is a way to make it easier to understand multi-level pointers as arrays. The first-level pointer is an one-dimensional array, the second-level pointer is a two-dimensional array, the third-level pointer is a three-dimensional array, and so on. Of course, sometimes it can not be understood in this way, so it is necessary to analyze the specific problems.

Int * ptr1; / / one-dimensional array int * * ptr2; / / two-dimensional array int * ptr3; / / three-dimensional array here, I believe you have a deeper understanding of "C language variable types and how to use", might as well to practice it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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