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

Lie C speech variables and data types

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

It is mentioned in the section "binary thinking and data storage":

The data to be disposed of by the computer (such as numbers, text, symbols, graphics, audio, video, etc.) are stored in memory in a binary way.

We call 8 bits (Bit) as one Byte and use bytes as the smallest operable unit.

We might as well start with the most complex integers and see how they are put in memory.

Variable (Variable)

In our ideal life, we will find a small box to store our belongings, which is not so confusing and convenient to find in the future. Computer is the same thing, we need to first find an area in memory, the rules use it to store integers, and give a memorable name to facilitate future search. This area is the "small box", we can put the whole number out.

Find an area in memory like this in C language:

Int a

Int is also a new word, it is the abbreviation of Integer, meaning integer. An is the name we gave to this area; of course, it can also be called by other names, such as abc, mn123, etc.

Find an area in memory, name it a, and use it to store integers.

Notice that there is a space between int and a. They are two words. Also notice the original semicolon, int a, which means intact, is a sentence that ends with a semicolon.

But int a; just find an area in memory where you can keep integers, so how do you release numbers like 123,100,999?

Put integers into memory like this in C language:

Axiom 123

= is a new symbol, which is called "equal sign" in mathematics, for example, 1-2-3, but in C, this process is called Assign. An assignment is a process that puts data into memory.

Connect the following two statements:

Int astragalus 123

So I put 123 in an area of memory called a. You can also write a sentence:

Int axiom 123

The integers in an are not intact, they can be changed at any time as long as we need them. The way to change is to assign the value again, for example:

Int aisle 123ntra 1000

The second assignment will mask (erase) the loss of the first data, that is, the initial values in an are 9999123 and 1000 that no longer exist and can never be found again.

Because the value of a can be changed, we give it an abstract name, called Variable.

Int a; invented a variable a, and we call this process variable bounds. Given 123 to variable a, we call this process assigning values to variables; and because this is the first assignment, also known as variable initialization, or initial values.

You can define variables before initializing them, for example:

Int abc;abc=999

You can also stop initialization while defining, for example:

Int abc=999

The two methods are equivalent.

The position of variable definition

Under VC or VS, the variable bound is put at the end of the function; that is, there can be no other code before defining the variable. In GCC, Xcode, or in the use of GCC compiler C-Free, Dev C++, Code::Blocks, the definition of variables can be put in the arbitrary position of functions.

For example, the following code is mostly accurate under VC, VS, GCC, and Xcode:

# include int main () {int a = 10; int b = 1000; printf ("BianChengBang"); return 0;}

With a slight modification to the code, place the printf statement at the ends of the variables an and b, as follows:

# include int main () {int a = 10; printf ("BianChengBang"); int b = 1000; / / there is a printf statement return 0;} before the b variable

The revised code is accurate under GCC and Xcode, but faulty under VC and VS.

Why are different compilers different? what is the basic reason? We will explain in detail in the two sections "the defining status and initial values of C speech variables" and "two sets of norms of C language".

Data type (Data Type)

The data is placed in memory, the variable is the name given to this piece of memory, with the variable, you can find and use this data. But the result is, how should it be used?

We know that data such as numbers, text, symbols, graphics, audio and video are mostly stored in memory in a binary way, and there is no real difference between them. So, 00010000 should be understood as the number 16. What about the color of a pixel in the image, or do you still have to recover a sound? If there is no special indication, we do not know.

In other words, there are many ways to explain the data in memory, which must be affirmed before use; the following int a; indicates that this data is an integer and cannot be understood as pixels, sound, and so on. Int has a professional title, called data type (Data Type).

In a literal sense, data types are used to clarify the type of data, affirming the method of explaining data, so that computers and programmers will not have ambiguity. In the C language, there are many data types, such as:

Specify character type short integer type long integer single precision floating point double precision floating point untyped data type charshortintlongfloatdoublevoid

These are the most basic data types, which are included in the C language, and if we need them, we can use them to form more complex data types, which we will explain one by one.

Continuous definition of multiple variables

In order to make the writing of the program more concise, C language supports the continuation of multiple variables, such as:

Int a, b, c; float m = 10.9, n = 20.56; char p, Q ='@'

Multiple variables are separated by commas and have opposite data types; variables can be initialized or uninitialized.

Length of data (Length)

The so-called data length (Length) means that the data occupies several bytes. The more bytes you occupy, the more data you can store, and the greater the value for numbers, and vice versa, the unlimited amount of data that can be stored.

Multiple data are continuously stored in memory, and there is no clear boundary between each other. If the length of the data is not clearly specified, the computer does not know when the access will be completed. For example, we keep an integer 1000, which takes up 4 bytes of memory, but reads it as 3 or 5 bytes, which is obviously not accurate.

Therefore, it is also necessary to specify the length of the data when defining variables. And this happens to be another effect on data types. In addition to indicating how the data is described, the data type also means that the length of the data is clearly understood. Because in C language, the number of bytes occupied by each data type is fixed, if you know the data type, you will know the length of the data.

In the 32-bit case, the lengths of various data types are generally as follows:

Specify character type short integer type long integer single precision floating point type double precision floating point data type charshortintlongfloatdouble length 124448

C language has several data types, each data type length is several, how to use, this is every C programmer must control, we will explain one by one later.

The initial summary

Data is stored in memory, access to data in memory to understand three tasks: where the data is stored, the length of the data and how to dispose of the data.

The variable name is not only a memorable name for the data, but also tells us where the data is stored. When using the data, we only need to provide the variable name, while the data type refers to a clear understanding of the length and disposal of the data. So ways such as int n;, char c;, and float money; confirm all the elements of data in memory.

C language provides a variety of data types to make the program more sensitive and efficient, but also adds the cost of further study. While some programming languages, such as PHP, JavaScript, etc., do not need to specify the data type when defining variables, the compiler will actively push the data type according to the assignment status, which is more intelligent.

In addition to C language, Java, C++, C # and so on must also specify the data type when defining variables. Such programming language is called strongly typed language. While PHP, JavaScript and so on do not need to specify the data type when defining variables, compiler fragments will actively deduce, such programming language is called weakly typed language.

Once strongly typed speech affirms the data type, it cannot be assigned to other types of data unless the conversion of the data type is stopped. There is no such restriction in weakly typed language. A variable can be assigned to an integer and then to a string.

The initial requirement is that the data type is only specified when defining the variable and must be specified; when the variable is used, there is no need to specify, because the data type has been confirmed at this time.

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

Network Security

Wechat

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

12
Report