<c Traps and defects >

Source: Internet
Author: User

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 >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.