The compiler is responsible for decomposing the program into a part of a symbol, commonly referred to as the "lexical analyzer." In C, whitespace between symbols (including space characters, tab characters, or line breaks) is ignored.
1, = different from = =
The C language uses the symbol "=" as the assignment operator, and the symbol "= =" as the comparison. The relative operations of assignment operations appear more frequently, so the symbol "=" with fewer characters is given a more common meaning-assignment operation. The assignment symbols in C are treated as an operator, so repeating assignment operations can be easily written, and assignment operations can also be embedded in larger expressions.
This ease of use can lead to a potential problem: When a programmer is meant to be a comparison operation, it can inadvertently be written as an assignment operation.
if (x = y) break;
The sentence is intended to check if x is equal to Y, but actually assigns the value of Y to X, and then checks whether the value is zero.
while (c = ' | | | c = = ' \ t ' | | c = = ' \ n ') c = getc (f);
because programmers are comparing"'and VariablesCwhen the comparison operator is mistakenly"=="is written as an assignment operator"=". Because the assignment operator"="Precedence is lower than logical operators"||", so the value of the following expression is actually assigned to theC:
"| | c = = ' \ t ' | | c = = ' \ n '
because"'Not equal to0 (ASCIIvalue is), then regardless of the variableCwhy the previous value, the result of the above expression evaluation is1, the loop goes on until the end of the file.
We should make comparisons explicitly and should not simply turn off the warning option.
if (x = y) foo ();
Should write:
if ((x = y) = =!0) foo ();
If the assignment operator is mistakenly written as a comparison operator, it can also cause confusion:
if ((Filedesc = = open (Argv[i], 0)) < 0) error ();
because the value of the comparison operation expression is only 0 or 1 , so (Filedesc = = open (Argv[i], 0)) the value will always be the only 0 or 1 , it is impossible <0 and execute to error () function.
?
?
2, & and | different from && and | |
?
3. "Greedy method" in lexical analysis
?
4, Integral type constant
?
5, characters and strings
<c Traps and defects >