"C Pitfalls and Defects" Chapter One Partial Questions Collected Thoughts

The road is hindered and long, and the line is coming. Keep your head down and work hard, if you don't talk, you will be a blockbuster! Come on, Sao Nian!

1 Exercises 1-3

1.1 Subject information

  The topic is as follows:

为什么 n-->0 的含义是 n-- > 0,而不是 n- -> 0?

1.2 Question thinking

  The compiler is found in order from left to rightMatching operator, I personally think that it has no practical meaning, only has practical meaning, and it seems to be a usage of pointers, generally it is not used in this way. n- n-- ->

1.3 Book answers

  According to the "big mouth method" rule, before the compiler reads it, it will be regarded as a single symbol. > --

1.4 Additional instructions

  • What is the "big mouth method" rule?

  The way the compiler decomposes the program into symbols is to read in one character by one from left to right. If the character may constitute a symbol, then read the next character to determine the character composed of the two characters that have been read Whether the string may be a component of a symbol: If possible, continue to read the next character and repeat the above judgment until the string composed of the read characters is no longer possible to form a meaningful symbol.

  This processing strategy is sometimes called the "greedy method" or, more colloquially, the "big mouth method".

2 Exercises 1-4

2.1 Subject information

  The topic is as follows:

a+++++b的含义是什么?

2.2 topic thinking

  Normally I don't write it like this. If I have to say the meaning of this sentence, analyze it according to the big mouth method, it should be , that is , but it seems that it is not very useful. a++ ++ + b ((a++)++) + b

2.3 Book answers

  The explanation of the answer in the book is also analyzed according to the steps, let's take a look below:

  • The only meaningful way of parsing in the above formula is:, to write more clearly is a++ + ++b (a++) + (++b)
  • However, if we analyze according to the big mouth method, the result should be as we thought, namely:, that is a++ ++ + b ((a++)++) + b
  • However, this converted "legal" formula is grammatically incorrect
((a++)++) + b
  • why? The result of cannot be used as an lvalue, so the compiler will not accept it as the operand of the following operator. In this way, if we follow the rules of parsing lexical ambiguity, the analysis of the above example is grammatically meaningless. a++ a++ ++
  • Of course, in programming practice, it is prudent to avoid using similar structures as much as possible unless the programmer is very clear about the meaning of these structures.

3 summary

  • A brief understanding of the "big mouth method" rules;
  • Understand the basic sequence of the compiler in recognizing symbols, etc.;
  • After understanding some pits, you should avoid stepping on them in the follow-up;

If the content of the article is wrong, please comment / private message a lot of advice, thank you! If you think the content of the article is not bad, remember to click three links (like, bookmark, leave a message), your support is my greatest encouragement, thank you!

Guess you like

Origin blog.csdn.net/Fighting_Boom/article/details/107203276