C language entry: 04. Data Type, constant, variable, language entry 04
I. Data 1. What is data?
We are always dealing with data, such as weight data, blood pressure data, and stock price data. When we use computers, we will be exposed to a variety of data, including document data, image data, and video data, there are also text data generated during chatting on QQ, and file data downloaded by thunder.
2. Data Classification
Data stored in a computer can be divided into static data and dynamic data.
(1) static data:
● Concept: static data refers to some permanent data, which is generally stored on hard disks. Hard disk storage space is generally relatively large, and now the hard disks of ordinary computers are about GB, so the hard disk can store some relatively large files.
● Storage Duration: After the computer is turned off, the data is still on. As long as you do not actively delete the data or the hard disk is not broken, the data will always be there.
● What is static data: static data is generally stored on hard disks in the form of files, such as documents, photos, and videos.
(2) Dynamic Data (temporary data)
● Concept: Dynamic Data refers to the temporary data dynamically generated during the running of the program, which is generally stored in the memory. The memory storage space is usually relatively small. Currently, the memory of a general computer is only about 4 GB. Therefore, use the memory with caution and avoid occupying too much memory space.
● Storage Duration: After the computer is disabled, the temporary data will be cleared.
● What is dynamic data: when running a program (software), the entire program will be loaded into the memory, and various temporary data will be generated during the program running, the temporary data is stored in the memory. When the program stops running or the computer is forced to shut down, all temporary data generated by the program will be cleared.
You may ask: Since the storage space on the hard disk is so large, why not load all applications into the hard disk for execution? The main reason is that the access speed of the memory is N times faster than that of the hard disk.
What data do programmers care about most?
(3) Conversion of static and dynamic data
Static> dynamic
Dynamic> static
3. Data size
(1) Both static and dynamic data are composed of 0 and 1. How does 0 and 1 make up so much data?
(2) If all data has size, static data will occupy hard disk space and dynamic data will occupy memory space.
(3) The larger the data, the more 0 and 1 contained, the bit and byte
(4) 1 KB = 1024 B, 1 MB = 1024 KB, 1 GB = 1024 MB, 1 TB = 1024 GB
4. Various data in the app
5. Data Types in C Language
Due to the wide variety of data in the app, C language data is classified to facilitate data operations.
2. Constant 1. What is a constant?
Constant, indicating some fixed data
2. Constant Classification
(1) integer constant (int)
Contains all integers, such as 6, 27, 109, 256,-10, 0, and-289.
(2) float type constant (float \ double)
Float constants are classified into double and float data types.
● Double: double Floating Point type, which is actually decimal. For example, 5.43,-2.3, and 0.0 (note that 0.0 is a decimal number)
● Float: Single-precision floating point type, which is also a decimal point, is less accurate than double, that is, it can represent less decimal places. To distinguish it from double, float data ends with f, such as 5.43f,-2.3f, and 0.0f. Note that the 10f format is absolutely not supported. the compiler will directly report an error and only allow f to be added to decimal places.
(3) character constant (char)
● Place a number (0 ~ 9), English letters (~ Z, ~ Z) or other symbols (+ ,-,! ,? And so on. For example, '6', 'A', 'F', '+', and '$.
Note: single quotes can only contain 1 character and cannot contain Chinese characters. The following statements are incorrect: 'abc', '123', and 'mal'
(4) string constants
● Enclose one or more characters in double quotation marks ("") to form a String constant. For example, "6", "male", "Wow haha", "abcd", and "my_car4", in fact, printf ("Hello World"); "Hello World" in the statement is a String constant.
What are the differences in usage between 6, '6', and "6? This is not discussed first. We will introduce it later.
3. Exercise
What types of constants are listed below?
10.6 1910000f 0.0 0 'A' "male" mj "294 '+'
3. Variable 1. What is a variable?
When the value of a data item needs to be changed frequently or uncertain, It should be represented by a variable. For example, game points.
2. Define Variables
(1) Purpose
● Any variables must be defined before use.
● The purpose of defining variables is to allocate a storage space to the variables in the memory to facilitate future data storage.
● If multiple variables are defined, different buckets are allocated for these variables.
(2) Format
Variable name;
For example, int num;
● The variable name is an identifier.
● Variable type
● Different types of variables occupy different storage spaces. Memory is extremely limited and proper storage space is allocated
● Type of data stored in the constraint variable (easy operation)
(3) Instances
int main()
{
int i;
char c;
int a, b;
return 0;
}
3. Use of Variables
(1) Assignment
● Save something to the variable, that is, assign values. Assign a value with a semicolon;
I = 10;
Note: The equal sign = here is not equal in mathematics, but the value assignment operator in C. It is used to assign the constant 10 on the right to the variable I on the left.
● The first assignment can be called "initialization"
● Two initialization Modes
● Define first, and then initialize: int a; a = 10;
● Initialize the definition at the same time: int a = 10;
(2) modify
● You can modify the value of a variable and assign values multiple times. Each assignment will overwrite the original value.
I = 10;
I = 20;
The last value of variable I is 20.
● Use printf to output the value of one or more variables
Int a = 10, c = 11;
Printf ("a = % d, c = % d", a, c );
● Double \ float \ char output, some tips for formatting characters
Double height = 1.55;
Char blood = 'a ';
Printf ("height = %. 2f, blood type: % c", height, blood );
● Simple addition and subtraction
Int a = 10 + 20;
● Do not use it when Initialization is not performed (the following statement is not recommended)
Int score;
Printf ("score = % d", score );
(3) Transfer of values between variables
● You can assign a value to another variable.
Int a = 10;
Int B =;
● Continuous assignment
A = B = 10;
4. Common Errors
(1) The variable name is the same as int a = 10; int a = 12;
(2) The variable scope is incorrect.
● Creation and release of Variables
● Code block scope {int a = 10 ;}
5. Exercise
(1) values of Integer Variables a and B are exchanged. For example, a = 10 and B = 11. After the exchange, the value of a is 11 and the value of B is 10. Two methods are used:
● Use third-party Variables
Int temp;
Temp =;
A = B;
B = temp;
● No third-party variables are used
A = B-;
B = B-;
A = B +;
(2) How many variables need to be defined on the observed game interface?