Principle of auto-increment and auto-increment operations

Source: Internet
Author: User

Int A = 1;

Int B = 1;

A ++ B;

A --- B;

Some people often ask what the values of these expressions are ..? What is the basis for analyzing such expressions ...? Let's discuss this content...

 

Here we need to talk about the greedy method... in <C traps and pitfalls>, we illustrate the rule that every symbol should contain as many characters as possible.

The compiler is analyzingProgramWhen reading a single character from the left to the right, how can we parse a string like "/* test ..? According to the rules, the first read must be the character '/', so you should try to include as many characters as possible and try to include the character '*', as a result, they can form a valid string "/*". The next character is 'T', Which is meaningless when combined with the preceding string, therefore, "/*" is the longest and meaningful string that can be combined. therefore, the resolution result is displayed. "/* test" should be resolved to "/*", followed by "test" and "test" to continue parsing according to the above rules... this is the process of lexical parsing, that is, the greedy method ".

 

After understanding the principle, let's look at the expression a ++ B; how should we resolve it:

Also according to the rules:

The next character is the character '+'. According to the principle of containing as many characters as possible, the following one is also the character '+', and the combination is "++ ", inC LanguageIs the auto-incrementing operator, so it is completely valid, so it parses "A ++", and so on, according to the rules, the next step will be resolved to "++". Here there is a problem...

It should be known that in C language, a ++ cannot be used as the left value. Therefore, the rule is resolved to "++", and the whole should be "(a ++) ++ ", but because a ++ cannot be used as the left value and violates the C language syntax, it cannot be parsed as '+, now it is parsed into "(a ++) + (++ B )"...

So the results come out on your own.

 

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.