C starts from scratch (2)
What kind of expression
This article is the beginning of this series. When learning English, the first time is a letter, which is the foundation of English. Similarly, in C , all code is composed of Identifier, Expression and statement, and some necessary symbols (such as braces, etc.), where the identifier is explained first.
Identifier
The identifier is a letter sequence, which consists of case English letters, underscores and numbers for identity. The identification is marked and identified, that is, the name. It can be used as a name that will be mentioned later or a function or class or the like, that is to say to identify a particular variable or function or element in C , etc. For example, ABC is an legitimate identifier, that is, ABC can use the name of the variable, function and other elements, but does not mean that ABC is the name of a variable or function, and the so-called legal is that any identifier must not be numbered. At the beginning, you can only include case in cases English letters, underscore, and numbers, you can't have other symbols, such as,! ^, Etc., and cannot be the same as C keywords. That is, when we gave a variable or function name, we must see the name as an identifier and must meet the requirements mentioned above. If 12AB_C is not a legal identifier, we cannot give a number of variables or functions to 12AB_C; AB_12C is a legal identifier, so it can be used as the name of variables or functions. The keyword mentioned in front, in the introduction of subsequent statements and some declaration modifiers, C provides some special identifiers as the name of the statement, to identify a particular statement, such as if, while, etc. Some modifiers are used to modify variables, functions and other elements to implement semantic or provide some specific information to compilers and connectors to optimize, check, and other operations such as Extern, Static. So when named variables or functions or other elements, this C keyword such as IF, Extern is not used as the name, otherwise the compiler will not be confirmed to be a variable (or other C element) or a statement, which cannot be compiled . If you want an identifier to be a specific variable or function or a class name, you need to use a declaration, which is described in detail in subsequent articles.
digital
C as a computer programming language, the computer is a digital, so the foundation in C is a number. Two numbers are available in C : integer and floating point numbers, that is, integers and decimals. However, since the computer is not imaginary (for details, see "C from zero (3)" section), the integer number is divided into symbol and unsigned integer, while floating point numbers The difference between the accuracy is divided into single-precision and double precision floating point, and the same integer number is also divided into length and short intellectuals according to length. To represent a number in the C code, write the numbers directly, such as: 123, 34.23, -34.34, etc. Since the computer is not based on the number-based, the C provides a series of suffixes in order to show, as follows:
u or u indicate that the number is unsigned, such as: 123U, but does not mean that the long integer or short integer L or L indicates that the number is long integer, such as: 123L; 123UL is unsigned long Type; 34.4L is a long double precision floating point number, equivalent to the double precision floating point number I64 or I64 represents a long-term number of long intensive numbers, which is a 64-bit operating system defined, and the length is longer than long. Such as: 43i64 f or f Represents numbers are single-precision floating point numbers, such as: 12.3f e or e represents the number of numbers, such as: 34.4e-2 is 0.344; 0.254E3F means a single-precision floating point number, value 254.4 When the suffix is not written, the specific type is determined according to the non-small number and the number of digits, such as: 123 indicates a symbol integer, and 12341434 is a symbolic length; and 34.43 represents the number of double precision floating point. . Why do you want to do so much? What is the symbolic symbol? This is all because the computer is not digital, but based on the state, details will be described in detail in the following.
As a scientific calculation, it may often encounter non-ten credit numbers, such as 16, 8-based, etc., C also provides some prefix for this.
In front of the digital, 0x or 0x indicates that this number is 16-en-system, such as: 0xF3fa, 0x11cf. In the front, one 0 means that this number is represented by 8, such as: 0347, becoming a decimal number of 231. However, 16 binders and 8 benefits cannot be used to represent floating point numbers, and can only represent an integer, that is, 0x34.343 is wrong.
String
In addition to providing numbers, C also provides characters and strings. This is entirely only available for easy writing programs, C as computer language, does not provide the necessity of strings at all. However, since the basic requirements for the computer are the results of the display, the characters and strings are used for display results because they are readily read symbols, so C provides support for strings. As mentioned earlier, the computer only knows numbers, and characters are text symbols, which is a graphic symbol. In order to make the computer can handle the symbol, the symbol must be made into a number in a manner, which is implemented by establishing a mapping between symbols and numbers, that is, a form. There are two columns in the form, one is the graphic symbol we want to display, and the other is a number, and the map can be established between graphics symbols and numbers by such a table. A standard table now is defined, called an ASCII code table, almost all computer hardware supports this conversion table to turn the number into symbols to display the calculation results. With the above table, when you want to explain that the result is "A", check the ASCII code table, get the number of "A" graphic symbols to be 65, then tell the computer output serial number 65, the last screen is displayed "A". This is obviously abnormal, providing characters and strings for this C . When we want to get the serial number of a graphic symbol's ASCII code table, you only need to enclose the character by single quotes, such as: 'a', the effects and 65 is the same. When you use more than one character, you will have multiple characters with a double quotation, which is the so-called string, such as "ABC". So the string is a plurality of characters. However, according to the previous description, it is easy to find that the string also needs to be mapped into numbers, but its mapping is not as simple as characters, and it will be made by checking the table. For this, the array will be introduced after the array is introduced after the array is described. . Operator
The basics of the computer are numbers, then all the operations of the computer have changed numbers, so normal C provides some basic operations of operational numbers, called Operator, such as - * /, etc. Any operator returns a number called an operator's return value, so the operator is an operational number and returns a number of symbols. As a generally classified, the number of numbers simultaneously operated in the operator is divided into one dollar, binary and three yuan operators. One yuan operator is:
After the numbers are connected, it returns the backward numbers. If the return value of 4.4F is 4.4; - 9.3F The return value of -9.3 is -9.3. It is exactly the need for semantics, as indicated that this is positive. - After the number, the symbol of the subsequent numbers is reversed. If the return value of -34.4F is -34.4; - (- 54) The return value of 54 is 54. Used to indicate a negative number. ! Later thereafter, logic reflects the number of numbers. The logical value is "true" or "false", in order to represent the logical value with a number, specify in C , the non-zero value is logical, and the change is logical. Therefore, 3, 43.4, 'A' represent logic, and 0 represents logic fake. Logic values are applied to subsequent judgment and circulatory statements. The logical reflector is first judged "!" The number of numbers received is logical or logic, and then the corresponding value is reversed. Such as:! 5 The return value is 0, because it is known to be 5 non-zero, then take the anti-logic vacation, so it will finally return 0. !! 345.4 The return value is 1, the advancement is 345.4 non-zero logic, reckless, logic, and then retrore the logic. Although only non-zero is logical, the logic returned as a compiler is true, and it will use 1 to represent the logic. ~ Later thereafter, reflect the number of numbers. Reverse is the operation defined in the logic and cannot be applied to the number. In order to reverse operation on digital applications, the digit is represented by binary, and then each bit of the number is reversed (because each bit of the binary number can only be 1 or 0, just in line with logic true and false ). The return value of ~ 123 is -124. First transfer 123 to a binary number 01111011, then each bay is reflosed to 10,000100, and finally -124. The problem here is why 8 bits rather than 16-bit binary. Because 123 is less than 128, it is positioned as a char type, so it is 8 bits (regarding what charisms will be described below). If it is ~ 123 ul, the return value is 4294967172. Why do you have a number to reverse this operation? Because the CPU provides such an instruction. And it is also very good and very important application, will be introduced later. Regarding other one-dollar operators will be mentioned in subsequent articles (but not necessarily mentioned). Binary operators are:
- * /% front and rear, one number, return two numbers, poor, accumulation, business, and remainder. If the return value of 34 4.4F is 38.4; the return value of 3 -9.3F is -6.3. The return value of 34-4 is 30; the return value of 5-234 is -229. 3 * 2 return value is 6; 10/3 return value is 3. The return value of 10% 3 is 1; 20% 7 return value is 6. && || The front and rear logical values, return the "or" operation logic value and "or" arithmetic logic value of the two logical values. Such as: 'a' && 34.3f The return value is logical, and the return value of 34 && 0 is logic fake, 0. 0 || 'b' The return value is logical, and the return value of 1; 0 || 0 is logic fake, 0. & | ^ One of them before and after returning two numbers "and" calculation, "or" operation, "different or" operation value. As mentioned earlier, first turn the numbers on both sides into a binary, and then perform, or, different or operations. Such as: 4 & 6 return value is 4, 4 to 00000100, 6 to 00000110 points, 00000100, 4. 4 | 6 return value is 6, 4 to 00000100, 6 to 0000010 points or have, 00000110, 6. The return value of 4 ^ 6 is 2, 4 to 00000100, 6 to 00000110 Different or have to be obtained, 00000010, 2. > <==> = <=! = The front and rear numbers are connected, depending on whether the two numbers are greater than, less than, equal to or equal to or equal to or equal to the corresponding logic value. If the return value of 34> 34 is 0, it is logical false; the return value of 32 <345 is 1, which is logical. 23> = 23 and 23> = 14 The return value is 1, which is logical; 54 <= 4 return value is 0, which is logic. 56 == 6 The return value is 0, for logic, the return value of 45 == 45 is 1, which is logical. 5! = 5 The return value is 0, which is logical fake; 5! = 35 return value is true, for logic. >> << The numbers of the left digit right or left shift right or left shift or left shift or left shift. Like the previous ~, &, |, etc., the reason why the left shift is provided, and the right shift operation is mainly because the CPU provides these instructions, mainly for compiling some algorithms based on binary number. << Turn the number of the left to the binary, then move the number of digits of the right value to the left, such as: 4, turn to 00000100, to move the left to the left, turn into 00010000, to get 16. >> Like <<, just move to the right. Such as: 6, turn to 00000110, shift 1 bit right, become 00000011, 3. If the 2 digits are shifted, one is exceeded, and the return value of 6 >> 2 is 00000001, 1. What is the use of left shift and right shift? It is used for some binary algorithms, but it can also be used as a simple optimization.
Considering the decimal number 3524, we turn left 2 digits, becoming 352400, expanded 100 times larger than the original number, accurately, should be two times twice to be expanded. If 3524 is shifted 2 bits, it becomes 35, which is equivalent to the original number divided by 100. Similarly, the more of the first 4 >> 2, equivalent to 4/4; 32 >> 3 corresponds to 32/8, i.e., the 32 division divided by 3 times. 4 << 2 is equivalent to 4 * 4, which is equivalent to 4 multiplying 2 by 2. Therefore, the left shift and right shift correspond to multiplication and division, but it can only be multiplied or divided by the number of corresponding numbers, but its running speed is much higher than the multiplication and division, so it is a simple Optimization means. The front and rear numbers are simply returned to the numbers on the right side. The return value of 34.45F, 54 is 54; the return value of -324, 4545F is 4545F. What is it used below? Used to make multiple numbers and into a number, will be further described in "C from zero (4)". Regarding other binary operators will be mentioned in subsequent articles (but not necessarily mentioned). There is only one three-yuan operator. To?:, Its format is:
The return value of 34? 4: 2 is 4, because 34 non-zero, the logic is true, return 4. The return value of 0? 4: 2 is 2, because 0 is logical fake, return 2.
expression
You should find the ridiculous place in front - 12> 435 return value is 0, why don't you write directly 0 and have a 12> 435? This is the meaning of the expression. The front and rear numbers of ">", but the operator is an operational number and returns a number of symbols, because it returns a number, so it can be placed on the place that is required to pick up the number, the so-called expression. The return value of: 23 * 54/45> 34 is 0, because the return value of 23 * 54 is 1242; then 1242 as the left number of "/", get a new return value 27.6; finally 27.6 as " > "The left number is further obtained to return value 0, which is logical. Therefore, the expression is a piece of code that is combined by a series of things that returns a number, which is composed of operators, so a certain return value. The "Return to the Number" can be another expression, or a variable, or a function with a return value, or an object having a digital type operator overloaded, as long as it can return a number thing. If you are strange for these nouns, functions, functions, classes, etc., do not need to be worked, will be described in successive articles. Therefore, 34 is also an expression that returns a value of 34, but it is only an expression that does not operate (will be understood later. 34 is actually an operator). Therefore, the concept of expressions is actually very wide, as long as there is something that returns the value, it can be referred to as an expression. Since there are many operators in the expression, the order of execution operators depends on the priority of the operator, as in mathematics, *, / / priority greater than , -, and , and greater than>, The previously mentioned logical operators "&&", "||", "|", as a logic, it is not worth considering by C . But why do you have such a type of operator that converts the number to a binary, and then performs a logical operation of the binary number? The first is that the CPU provides the corresponding instructions, and it is also a very meaningful application below. Considering the junction, there are three traffic lights in each intersection, indicating whether the left turn, right turn straight. There are 12, now you have to write a control program for it, no matter what the function of this program, you first need to convert the status of the traffic light into a number because the computer only knows the number. So use three numbers to represent the three traffic lights of a certain intersection, so each traffic light is represented by a number, assuming that the red light is 0, the green light is 1 (regardless of yellow light or other case). Later, I suddenly found that it can also use a number to represent the three traffic lights of an intersection. If 110 is used to deeper left green light, straight green light and right red light. The above 110 is a decimal number, and each of its actual can be 0 ~ 9 ten numbers, but it is only available to two: 0 and 1 here, it feels very wasteful. Therefore, the number of binary is selected, or 110, but it is a binary number, and the number of revolutions is 6, even when it is 111, it is only 7, which is much smaller than the previous 110, saves ... ...? ? what? ? We write numbers on paper 235425234 must be more compared to write 134 this number to take more paper (assuming words are as large). Therefore, a big number is recorded more resources to record a small number. It's ridiculous! Whether it is 100 or 1000, it's just a number, why do you record a large number of resources? Because the computer is not a digital computer, it is an electronic computer, it is based on a state rather than a numeric, which will be described in detail in the following. The computer must use some representation to represent a number, and that means of means of binary, but not binary, so there is a smaller number of records, which is why the above integer What long is a long-term short integer. Let's continue the above thinking below. Using 110 binary numbers to represent the state of three traffic lights, then now know what the number of 110 this figure represents the left turns red green light. The left turn is indicated by the third digit, but the computer does not know this, so as follows: 110 & 100. The return value of this expression is 100, non-zero, logical. Assuming a certain intersection is 010, the same 010 & 100, the return value is 0, logic fake. Therefore, use the "&" operator to extract a certain bit or several bits in the binary number. So we have to understand whether the left and red green light in the traffic light of a number represented by a digital representative will only let it be with 100. Now keep the state of other traffic lights unchanged, only the left turns red green light is green, such as 010, in order to make the left turn red green light, the value should be 110, which can be done by 010 | 100. If the current state is 001, 001 | 100 is 101, the correct - straight and right turn of the traffic light state does not change. So use the "|" operator to give a certain bit or several bit setting status in a binary number, but can only be set to 1, if you want to set it to 0, such as 101, to turn off the left turn green light, 101 & ~ 100, the return value is 001.