C from zero (3) - What is the variable of this document that the content is the key to C . Basic most people are fainting in these content, but these content is the basis of programming, and must be detailed. The number represents mathematics, the number is only different, and there will never have the difference between the numerical space, that is, the number of mathematics is a logical concept, but the computer is not. Considering the abacus, there are many listing operators on each abacus, and each column is divided into upper and lower two-row operator. There are 2 aluminum albums, each representing 5, and there are 4, each representing 1 (this is not important). Therefore, each column on the abacus has a total of 6 operators, each column can also represent the 15 digits of 0 to 14 (because the possible state of the upper alignment is valid, and the lower alignment) may have 0 to 4 operators are valid, so as 3 × 5 = 15 combined modes). The above focus is that each column of the abacus does not represent 15 numbers of 0 to 14, but there are 15 states per column, so it is used to represent numbers (this is important). Because each column of the abacus has 15 states, there may be 15 × 15 = 225 states with two list operators, and therefore 0 to 224 can be represented. Each of the Arabic numbers has 0 to 9 these 10 graphics symbols, and it can have 10 × 10 = 100 states when using two Arabic digital graphics symbols, so 100 numbers of 0 to 99 can be represented. The abacus here is actually a 15-based deciple (can record a number of numbers by maintaining a state of one list), and one of its operating operators is equivalent to a Arabic number, 15 states per column, Therefore, it is possible to represent 15 numbers from 0 to 14, and must be joined to the addition of another column operator after being entered after being exceeded 14. The computer is like this, which is not a digital computer, but an electronic computer, a number of potentials in a computer through a line. The potential in one line specifies only two state-high potentials and low potentials, so the digital representation of the computer is binary. As with the above abacus, only two states are to indicate that when the number exceeds 1, it must be carried out to refer to the addition of the other line to represent numbers. The so-called 32-bit computer is to provide 32 lines (called data bus) to represent data, so there are 2 to 32 times. The 16 lines can represent 2 of the 16th states. Therefore, the computer is not based on the number of binary, but based on the state change, only this state can be expressed using the binary number. That is, the computer does not know the binary number, which is the basis of the "Type" section below. Memory memory is the hardware that can record numbers in the computer, but its storage speed is very fast (compared to low-speed storage devices such as hard drives), but it is often saved for a long time, so it is often used as a draft paper to record some temporary information. As mentioned earlier, the number of 32-bit computer is represented by a combination of potential states on the 32 line, so the memory can record the number, that is, the respective potential status of 32 lines (just as the calculation of the abacus) After the child is dial, it will not change the position unless it is again dialing it). However, considering the above abacus, if there is a 15-yard operating calculator on an abacus, an abacus can represent 15 square status, which is a lot of numbers, but it is often not used to change so large numbers. Therefore, it is only two other operators that can only represent 225 states, and another or more abacus is used when the number is exceeded.
No matter whether it is a 2-rated operating calculator or a grade of the calculator, it is the granularity of the abacus, and the particle size is excessively caused. It is unnecessary waste (many 列 列 算 子 不 不), too small and very troublesome (multiple abacus) . The computer is like this. 2 The number of 32 times can be represented, generally will not be used, and if it is directly stored in memory directly, it will cause a considerable resource waste. Thus, as described above, the particle size of the memory is 8-bit binary, referred to as a memory unit, and its size is called one byte (byte). That is to say, memory storage figures, at least recorded the potential state on the 8 lines, which is 256 states in 8 squares. So if a 32-bit binary number is stored in memory, it is necessary to occupy 4 memory cells, which is the 4 bytes of memory space. We write on paper, judge the relative abscissa and ordinate of the paper on paper through the naked eye to find the word to be viewed or to write. Similarly, since the memory is equivalent to draft paper, some positioning method is also required to position, in the computer, it is positioned by a number. Like the room number of the hotel, the memory unit is equivalent to the room (assuming that each room can only stay alone), and the number said in the front is equivalent to the room number. In order to write data to a block (that is, using a block memory to record the potential state on the data bus), the number corresponding to this memory must be known, and this number is called the address. The corresponding memory cells found through a given address are called addressing. Therefore, the address is a number that is used to uniquely identify a particular memory unit. This number is generally 32-bit long binary numbers, and it can also represent 4G state, that is, the general 32-bit computer has 4G memory space addressing capabilities, that is, the computer is up to 4G memory, if the computer has more than 4G Memory, at this time, it is necessary to increase the length of the address, and is represented by a 40-bit long binary number. When the type is the most beginning to this series, what is the programming, and the computer has just not explained that the computer does not know, but the status record, and the so-called addition is just the manual design of the adder to make two states. The status of the device is the same as the result of the mathematical addition. Everything is only illustrated: What is the job made by the computer, what is the people who use it all? Therefore, in order to use the computer that quickly "calculate" capability (actually state transform capability), artificially specifies how to explain those states. In order to facilitate it, for the state of the potential preceding potential, we use a 1-bit binary number to indicate that the state of the above can be used to use a binary number, and the so-called "explaining those states" becomes how to explain A binary number. C is an advanced language, in order to help explain those binary numbers, provide this concept. Type is how people develop a protocol that explains the binary number in memory. C provides some of the following standard type definitions.
Signed char indicates the number of numbers in the memory to use, indicated by -128 to 127, and the length is 1 byte unsigned char indicates the number of numbers in memory pointed to use the original code. 0 to 255, the length is 1 byte Signed Short indicates the number of numbers pointed to the number in memory, indicating the number of -32768 to 32767, the length of 2 bytes Unsigned Short represents the pointer to the memory. Digital uses the original form of origin, indicated by 0 to 65535, length is 2 byte signed long indicates the number of numbers in memory pointed to use, indicating the number of -2147483648 to 2147483647, length 4 words The NNSIGNED Long indicates that the numbers in the point pointers use the original code form, indicating the number of 0 to 4294967295, the length is 4 bytes Signed Int indicates the number in the memory pointed to use, and the number represented by the representation. translater. If the compiler is compiled, it is specified to compile to run on the 16-bit operating system, and it is equivalent to the signed short; if it is compiled into 32-bit, it is equivalent to Signed long; if it is compiled as running on a 64-bit operating system, then For 8 bytes, the scope can be calculated as described above. Unsigned int indicates that the numbers in the pointed memory are used in the original code, and the remaining and Signed Int are indicated by the unsigned number. Bool indicates that the numbers in the pointed memory are logical, and the value is false or true. The length is 1 byte. Float means that the memory pointed to interprets the IEEE standard, which takes up the 4-byte memory space, which is equivalent to the single-precision floating point mentioned in the upper. Double indicates that the memory pointed is interpreted by the IEEE standard. It can Repal * 8, which represents the number of accuracy than float, occupying 8-byte memory space, is equivalent to the double precision floating point of the second article. Long Double indicates that the memory pointed to the IEEE standard, which is REAL * 10, which represents the number of precision than Double, but when writing a program for 32-bit Windows operating systems, it still takes 8 bytes memory space, equivalent to Double, just if the CPU supports such floating point types, this accuracy can be performed. The standard type is more than a few, and there will be mentioned later. The above length is 2 bytes, which is to take out the numbers in the two consecutive memory cells and merge together to represent a number, which is not a number that the previous abacus can't represent, and carry another abacus help It is said that it is the same. The upper Signed keyword is removed, namely the char is equivalent to the signed char, to simplify the writing of the code. But it is also limited to Signed, if it is unsigned char, it will still be unsigned char. It should now be understood why the numbers have a symbolic unsigned, long-term short integer, and the above short, char, etc. are only different, which is based on the programmer The number of digital variations that appear will be selected. The type is just the interpretation of the numbers in memory, but the above type looks relatively simple, and semantics is not very strong, that is, there is nothing special. To this end, C provides a custom type, which is the structure, class, etc. will be described in the subsequent article.
The variables have been said in the first one of this series, the most of the work of computer programming is to operate the memory, and the above is said, in order to operate the memory, you need to use the address to identify the first address of the memory block to be operated (LONG above Represents a continuous 4 byte memory, the address of its first memory cell is referred to as the first address of this continuous 4 byte memory blocks). To this end, we must write down the address when writing a program. Do 5 2 / 3-5 * 2 calculations, first calculate the value of 2/3, write on draft paper, then calculate the value of 5 * 2, and written on draft paper. For the next addition and subtraction operation, it must be able to know which one of the two numbers on the draft paper is a value of 5 * 2. People are remembering the position of the two numbers on paper, while the computer is identified by the address. However, the computer only makes the addition and subtraction, and does not take the position of the intermediate values of those 2/3, 5 * 2, that is, the address. Therefore, the programmer must complete this work and record the two addresses. The problem is that there are only two values here, maybe I know some, but if more, people are very difficult to remember which value corresponds, but people are more sensitive to the symbol, which is easy to note. Name rather than a number. To this end, the programmer wrote a table itself, the table has two columns, one column is "2/3 value", a column is the corresponding address. If the formula is slightly complex, then the table may have a twenty or thirty lines, and every writing code is to go to the corresponding address, if you come to a tens of thousands of lines, it is not tolerated. C as a high-level language, it is normal to provide the solution to the above problem, which is to help the programmer to protect the table. When you check it, it is the compiler to check, which is the function of the variable. Variables are a mapping element. The table mentioned above is maintained by the compiler, and each line in the table is an element of this table (also known as record). Table has three columns: variable name, corresponding address, and corresponding type. The variable name is an identifier, so its naming rules are fully in accordance with the previous one. When you want to write data for a block, the programmer uses the corresponding variable name to make a memory identity, and the corresponding address in the table records this address, thereby giving the programmer, an identifier, mapping As an address, the variable is a mapping element. The corresponding type tells the compiler how should explain the memory pointed to by this address, is it two consecutive bytes or 4? Is it the original code record or complement? The contents of the memory identified by the address corresponding to the variable are called the value of this variable. There is a variable interpretation: "Variable amount, it is equivalent to a box, the number is installed in the box, and the variable name is written outside the box, so the computer knows which box we have to handle, and different boxes Different things, the box of the string cannot be installed. "The above is the first time learning programming, the book written (is a Basic language). For beginners may be easy to understand, they can't be said, but caused misunderstandings will result in a thousand holes in the future program. The above explanation implies a meaning-variable is a memory. This is a serious mistake! If the variable is a memory, then the famous reference type in C will be discarded. The variable is not actually a memory, just a mapping element, which is a truth. The memory has been said in front of the memory and its use, but the memory cannot be used casually, because the operating system uses memory, and now the operating system is normal, you can execute the multi-task operating system. Multiple programs even if there is only one CPU. So if you do not have a memory access, it may damage the operation of another program.
For example, I wrote a 2/3 value on the paper, and you wipe it off with I agree and not inform me, and write the value of 5 * 2, and all the calculations on my back will be wrong. Therefore, in order to use a memory, you need to apply to the operating system, and the operating system is unified manages memory used by the program. So in order to record a log type number, first apply for a continuous 4-byte long memory space to the operating system, and then the operating system will view in memory, see if there is still a continuous 4-word diverse memory, if If you find it, return this 4-byte memory first address, then the compiler is compiled in the previously mentioned variable table, and finally you can use it to record some temporary calculations. The above process is called the required operating system to allocate a piece of memory. This looks very good, but if you only ask the operating system for 4 bytes to search for the memory condition, if you need 100 temporary data, you will ask the operating system to allocate memory 100 times, which is clearly efficient (unneesed 99 times) View memory status). Therefore, C discovers this problem, and the operating system also proposes the corresponding solution, and finally puts forward the following solution. Stack (stack) Any program execution, pre-assigned a fixed length memory space, which is called a stack (this statement is not accurate, but since it actually involves thread, in order to complicate problems In this way, it is also called a stack. So when you ask for a 4-byte memory, it is actually getting memory in this allocated memory space, that is, the memory maintenance work is made by the programmer himself, ie the programmer himself determines which memory can be used, not the operating system. Until the assigned memory is used. Obviously, the above work is made by the compiler. It does not have to worry about the programmer, so it does not happen to the programmer's perspective, or you need to apply to the operating system as original, and then use. But work is just from the operating system to the program. It is necessary to maintain the memory, but the time to spend the CPU, but it is much more, because you don't use the memory whether there is anyone, and specifically record an address. The memory space above this address is being used, and the memory space below this address is unmanned. The reason why the following space is not used instead of or more, you can know the stack overflow when this address is reduced to 0 (if you already have some foundation, please don't think about the virtual memory address, about virtual memory Will explain in "C from zero (18)", this explanation is just to facilitate understanding). Moreover, the CPU also provides support for this method, gives two instructions, and the assembly language is PUSH and POP, indicating the stack and out of the stack, reducing and increasing that address, respectively. The most important benefit is that because the program has been assigned a large number of continuous memory when executed, use a variable to record the first address of this continuous memory, and then all the programs are used, the programmer is assigned to the operating system. The memory can be obtained by adding the corresponding offset by the first address, which is clearly made by the compiler.
Therefore, it is actually equivalent to the compilation period (ie, when the compiler compiler) has been assigned memory (note, the actual compile time cannot be assigned memory, because allocation memory is the program running to the operating system, and here Due to the use of the stack, the compiler will generate some instructions to apply to the operating system at the beginning, if the failure is returned, and if not withdrawn, it means that the memory has been assigned, and the code is used in the code. Move it to use memory is also effective), but the damage is only to allocate memory during compilation period. The work above the Heap is the compiler, that is, the programmer does not participate in the stack maintenance. However, the stack is equivalent to allocating memory during compilation period, so once the offset of a block is calculated, this memory can only be so large, can not change (if the change can cause the offset of other memory blocks) error). For example, if you ask the customer to enter order data, there may be 10 copies, or there may be 100 copies. If the start is fixed, the memory size may cause unnecessary waste, or the memory is not enough. In order to solve the above problem, C provides another way, that is, the programmer is allowed to have two ways to apply for memory to the operating system. The former one is to assign on the stack, and the memory size of the application is fixed. The latter is allocated on the heap, and the memory size of the application can be changed when running, not fixed. So what is a pile? Under the Windows operating system, the memory allocated by the operating system is called a heap, and the stack can be considered to be allocated at the beginning of the program (this is not accurate, but in order to unsuve problems, this description). Therefore, in the heap can allocate the memory block of size, because it is a molfer assigned in the run period, not the compiled memory block. The definition of the variable has so much, you may be very dizzy, after all, even one instance is not, all text, let's help deepen the understanding of the above. Define a variable, adding an element to the above-mentioned variable table maintained by the compiler, its syntax is as follows: long A; the type of the first write variable, then one or more spaces or tab (/ t) or other interval The name of the variable, finally ended with the semicolon. To define multiple variables at the same time, the comma is separated from each variable, as follows: Long A, B, C; UNSIGNED SHORT E, A_34C; the above is two variable definition statements, each statement is separated from the semicolon, and each The same type variable is separated from commas. The previous formula 5 2 / 3-5 * 2 is written as follows. Long a = 2/3, b = 5 * 2; long c = 5 a - b; can not remember the annoying address, just remember the simple identifier A, B. Of course, the above style is not necessarily written, or you can write: long c = 5 2/3 - 5 * 2; and the A, B and other intermediate variable compilers will automatically generate and use (actual compiler Due to optimization, the results will be calculated directly without generating actual calculation code). Below is the key to the problem, defining variables is to add a mapping. As mentioned earlier, this mapping is associated with a variable name and an address, so when defining a variable, the compiler corresponds to the variable name and an address, and helps the programmer allocated a memory on the stack mentioned earlier. The size will depend on the size of this variable type.