The part in the compiler that is responsible for decomposing the program into one symbol is called the "lexical analyzer ". Let's take an example: if (x> big) big = x; the first symbol of this statement is the keyword of the C language if, followed by the Left brackets, the next symbol is the identifier x, the next is the greater sign, the next is the identifier big, and so on. In C, spaces between symbols are ignored. This chapter discusses the relationships between symbols and the characters that make up the symbols, as well as some common misunderstandings about the meaning of the symbols. Trap 1 "=" is different from "=", which mistakenly writes "=" to "=", which is an easy-to-see error and is not easy to check. Let's take a look at the example program page6_7.c. The Code is as follows: [cpp] 1 # include <stdio. h> 2 3int main () 4 {5 int I, j; 6 I = 10; 7 j = 20; 8 if (I = j) 9 printf ("I equal j \ n"); 10 11 return 0; 12} 1 # include <stdio. h> 2 3int main () 4 {5 int I, j; 6 I = 10; 7 j = 20; 8 if (I = j) 9 printf ("I equal j \ n"); 1011 return 0; 12:
The first line of this program is to determine whether I is equal to j. If I is equal, print the statement. Now, "=" is written by mistake as "=", indicating that j is assigned to I, and if is used to determine whether I is 0. Therefore, unless the value of j is 0, the if judgment of the 8th rows is always true. For verification, you can try the following program running effect: [cpp] 1 # include <stdio. h> 2 3int main () 4 {5 int I, j; 6 I = 10; 7 j = 0; 8 if (I = j) 9 printf ("I equal j \ n"); 10 11 return 0; 12} 1 # include <stdio. h> 2 3int main () 4 {5 int I, j; 6 I = 10; 7 j = 0; 8 if (I = j) 9 printf ("I equal j \ n"); 1011 return 0; 12} some symbols in the greedy C language in the lexical analysis of trap 2, such as/, *, and =, only one character is long, called a single character, and there are also some symbols, such as/* And =, which contain multiple characters, called a multi-character symbol. When the C compiler reads one character "/" and then another character "*", the compiler must make a judgment: treat it as two single-character symbols or combine it as one character. The C language uses the greedy method (also known as the Big Mouth method) to solve this problem: each symbol should contain as many characters as possible. See the example page8_9.c. The Code is as follows: [cpp] 1 # include <stdio. h> 2 3int main () 4 {5 int a = 10, B = 2; 6 printf ("a = 10, B = 2, a --- B = % d \ n ", a --- B); 7 8 return 0; 9} 1 # include <stdio. h> 23int main () 4 {5 int a = 10, B = 2; 6 printf ("a = 10, B = 2, a --- B = % d \ n ", a --- B); 78 return 0; 9:
Rows 1, a --- B are analyzed by greedy method, which is equivalent to (a --)-B. Note that the first is a --, that is, first take the value of a and then perform the minus 1 operation, so 10-2 = 8. Before running, I think the result should be 7. Trap 3 integer constant if the first character of an integer constant is a number 0, the constant is regarded as an octal number. Therefore, 11 and 011 have different meanings. See the code page10_11.c: [cpp] 1 # include <stdio. h> 2 3int main () 4 {5 int a = 11, B = 011; 6 printf ("a = % d, B = % d \ n",, b); 7 8 return 0; 9} 1 # include <stdio. h> 23int main () 4 {5 int a = 11, B = 011; 6 printf ("a = % d, B = % d \ n", a, B ); 78 return 0; 9} The execution result is as follows: the execution result shows that 011 is regarded as the octal number and the corresponding decimal number is 9. Trap 4 characters have different meanings from single quotes and double quotation marks in string C language. In some cases, if you mix the two, an error occurs during compilation. Sometimes, the compiler does not report an error, this results in unpredictable results during running. A character enclosed in single quotes actually represents an integer. The integer corresponds to the Sequence Value of the character in the character set used by the compiler. Therefore, for compilers using the ASCII character set, the meaning of 'A' is strictly consistent with that of 0141 (octal) or 97 (decimal. A string enclosed in double quotation marks represents a pointer pointing to the starting character of an unknown array, the array is initialized by characters between double quotation marks and an extra binary value of 0 '\ 0. The following statement: printf ("Hello world \ n"); and char hello [] = {'h', 'E', 'l', 'l ', 'o', '', 'w', 'O', 'R', 'l', 'D', '\ n', 0}; printf (hello ); is equivalent.