Talk about C language variables and data types

Variable (Variable)
In real life, we will find a small box to store items. Firstly, it will not look so messy, and secondly, it will be convenient to find later. The same is true for computers. We need to find an area in the memory first, stipulate that it should be used to store integers, and give it an easy-to-remember name to facilitate future search. This area is the "small box", and we can put integers in it.

In the C language, find an area in the memory like this:
int a;
int is another new word, it is the abbreviation of Integer, which means an integer. a is the name we gave this area; of course, it can also be called other names, such as abc, mn123, etc.

The meaning of this statement is: Find an area in memory, name it a, and use it to store integers.
Note that there is a space between int and a, they are two words. Also pay attention to the semicolon at the end, int a expresses the complete meaning, it is a statement, and it needs to end with a semicolon.
But int a; just finds an area in memory that can store integers, so how to put numbers like 123, 100, and 999 into it?

In C language, put integers in memory like this:
a=123;
= is a new symbol, which is called "equal sign" in mathematics, for example, 1+2=3, but in C language, this process is called assignment (Assign). Assignment refers to the process of putting data into memory.

Connect the above two statements:
int a;
a=123;
put 123 in a memory area called a. You can also write it as a statement:
int a=123;
The integer in a is not static and can be changed at any time as long as we need it. The way to change is to assign again, for example:
int a=123;
a=1000;
a=9999;
the second assignment will overwrite (erase) the first data, that is to say, the last value in a is 9999, 123, 1000 no longer exist, and can never be found again.

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

int a; creates a variable a, we call this process variable definition. a=123; hand over 123 to the variable a, we call this process assigning a value to the variable; and because it is the first assignment, it is also called the initialization of the variable, or assigning the initial value.

You can define variables first, and then initialize, for example:
int abc;
abc=999;
You can also initialize while defining, for example:
int abc=999;
These two methods are equivalent.
Data Type (Data Type)
Data is stored in memory, and variables are names given to this piece of memory. With variables, this data can be found and used. But the question is, how to use it?

We know that data such as numbers, text, symbols, graphics, audio, video, etc. are stored in the memory in binary form, and there is no essential difference between them. So, should 00010000 be understood as the number 16, or the color of a certain pixel in the image, or should a certain sound be emitted? If not specified, we do not know.

That is to say, there are many ways to interpret the data in the memory, which must be determined before use; the above int a; indicates that this data is an integer and cannot be understood as pixels, sounds, etc. int has a professional name called data type (Data Type).

As the name implies, data types are used to describe the type of data and determine how the data is interpreted, so that computers and programmers will not have ambiguity. In C language, there are many data types, for example:
description character type short integer type long integer type single precision floating point type double precision floating point type untyped
data type char short int long float double void
These are the most basic data types, which come with C language. If we need, we can also use them to form more complex data types. We will explain them one by one later.
Continuous definition of multiple variables
In order to make program writing more concise, C language supports continuous definition of multiple variables, for example:
int a, b, c;
float m = 10.9, n = 20.56;
char p, q = '@';
multiple variables defined continuously are separated by commas, and must have the same data type; variables can be initialized or not.
Data length (Length)
The so-called data length (Length) refers to how many bytes the data occupies. The more bytes it occupies, the more data it can store, and for numbers, the value will be larger, otherwise the data that can be stored is limited.

Multiple data are stored continuously in the memory without obvious boundaries between each other. If the length of the data is not clearly specified, the computer will not know when the access ends. For example, we save an integer 1000, which occupies 4 bytes of memory, but when we read it, we think it occupies 3 bytes or 5 bytes, which is obviously incorrect.

Therefore, the length of the data should also be specified when defining the variable. And this is precisely another role of the data type. In addition to indicating how the data is interpreted, the data type also indicates the length of the data. Because in the C language, the number of bytes occupied by each data type is fixed. If you know the data type, you also know the length of the data.

In a 32-bit environment, the lengths of various data types are generally as follows:
Indicate character type short integer type long integer type single precision floating point type double precision floating point type
data type char short int long float double
length 1 2 4 4 4 8
How many data types are there in C language, what is the length of each data type, and how to use them. This is what every C programmer must master, and we will explain them one by one later.
Final summary
Data is stored in memory. To access data in memory, three things need to be clarified: where the data is stored, the length of the data, and how the data is processed.

The variable name is not only an easy-to-remember 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 indicates the length and processing method of the data. So forms such as int n;, char c;, float money; determine all elements of the data in memory.

The various data types provided by the C language make the program more flexible and efficient, but also increase the learning cost. And some programming languages, such as PHP, JavaScript, etc., do not need to specify the data type when defining variables, and the compiler will automatically deduce the data type according to the assignment, which is more intelligent.

In addition to the C language, Java, C++, C#, etc. must also specify the data type when defining variables. Such a programming language is called a strongly typed language. However, PHP, JavaScript, etc. do not need to specify the data type when defining variables, and the compilation system will automatically deduce it. Such a programming language is called a weakly typed language.

Guess you like

Origin blog.csdn.net/D0126_/article/details/131449863