Pre-prepared comments for "A + = A-= A *"

Source: Internet
Author: User
Document directory
  • Expression)
  • Expression effect
  • "A + = A-= A *"

The value assignment expression can also include a composite value assignment operator. For example:
Int A = 12;
A + = A-= A *
It is also a value assignment expression. If the initial value of A is 12, the procedure for solving the assignment expression is as follows:
① Perform the "A-= A * A" operation first, which is equivalent to a = A-A * A. The value of A is 12-144 =-132.
② Perform the "A + =-132" operation, which is equivalent to a = a + (-132). The value of A is-132-132 =-264.

The first thing to note is that the"Int A = 12;That line was added by the author. Because the definition of "a" is not explained (variable or constant? Data Type ?) Under the premise that the discussion itself is meaningless. To raise the wrong text to a level worth discussing, add"Int A = 12;"This prerequisite is necessary.

Errors in this section are hidden, and most C language learners are hard to find the problems. To thoroughly understand the problems, we need to first establish or clarify several basic concepts.

Expression)

What is an expression?

Simply put, an expression is a sequence composed of operators and operands. For example:

2 + 4

Of course, some expressions only have operands without operators. For example:

6

Not any sequence composed of operators and operands is a reasonable and valid expression. For example

(A = 3*5) = 4*3

Is an expression with obvious errors.

However, what rules should operators and operands follow to form a reasonable and valid expression is not clear in one or two sentences. So I will not elaborate on it for the moment.

An expression may be part of another expression. In this case, the former is the subexpression of the latter ).

Expression effect

Expressions in the code can have up to three effects:A computer is required to calculate a value, specify an object or function, and generate a side effect).

The expression "3 + 5" obviously requires computer evaluation. For

Int I;
For example, the subexpression "I" in the expression "I = 5" is used to specify the int type data object (object) named "I.

The so-called "Data Object" refers to the storage range of a data. In other words, the "I" here refers to the Space Range and content of a specific data storage.

There is a significant difference between the expressions "I = 5" and "3 + 5", that is, the former has two effects: evaluate the value of the expression (this value is 5 ); secondary effect-the "I" object is stored with a value of 5.

It can be seen that an expression in C language may have multiple effects. Sometimes we only use one of the effects, and sometimes we use several effects at the same time, which can make the code very concise. For example, in the following code segment

Int I;

Printf ("% d \ n", I = 5 );
The two effects of the expression "I = 5" are used: evaluate the value of "I = 5", and I is assigned 5 values. This method is obviously better

Int I;

I = 5;

Printf ("% d \ n", I );
It is much more concise and beautiful.

"A + = A-= A *"

After learning about the effects of expressions, you can analyze the expression "A + = A-= A *.

This expression has three operators: "+ =", "-=", and "*". "*" Has the highest priority. "+ =" and "-=" have the same priority and are both lower.

For expressions with the same priority operator, we need to check the combination of these operators to determine the true meaning of the expressions. The combination of "+ =" and "-=" is from right to left. In this way, you can know the operation objects of each operator.

First, "*" has the highest priority, so its operands are "a" and "A" in "A * ". The subexpression "A * A" has only one effect: Evaluate.

Second, the combination of "+ =" and "-=" is from right to left. Therefore, determine the operands of "-=" first. "-=" Is a binary operator, so its operation object is the previous "A" in "A-= A * A" and the subexpression "A * ". It is not hard to see that the "-=" operation does not have only one effect, but has two values: evaluate, and change the value in memory represented by "A * A" to the value of "A *.

Finally, it can be determined that the operand "+ =" is "A" on the leftmost side of "A + = A-= A * A" and the subexpression "a-= A * ". The effect is also the sum of the values that convert the value in the memory represented by "A" to the value of the expression "a-= A *.

Therefore, the original expression can be equivalent:

A + = (a-= (A * ))

The brackets added clearly show the operands of each operator. All the effects required by this expression can be summarized as follows:

  • The value of the subexpression "A *;
  • The value of the subexpression "a-= A *;
  • The value of the subexpression "A + = A-= A *;
  • Change the memory value represented by "A" to the value of the subexpression "A *.
  • Change the memory value represented by "A" to the value of the subexpression "a-= A *.

The first three items are values, and the last two items are the secondary effects of expressions.

It is certain that the computer will first evaluate the value of "A * A". Otherwise, the value of the subexpression "a-= A * A" cannot be obtained. It can also be determined that the computer will first evaluate the value of "A-= A * A", otherwise the value of the subexpression "A + = A-= A * A" cannot be obtained.

However, it cannot be determined when the secondary effect will be completed-the C language does not specify the time when the secondary effect will occur. The C language requires that the two subeffects be completed before the next sequence point. The concept of order points is not defined accurately at the moment. Simply put, if this expression forms an expression statement:

A + = (a-= (A * ));

Then ";" is a sequence point. The meaning is that the previous operation (including the sub-effect) must be completed here.

In this way, the time when these two sub-effects are generated. If different compilers have different arrangements, the value of the entire expression or the final value of the variable "A" will be generated, different compilation methods produce different conflicting results. However, as long as the sub-effects are completed before ";", each compilation "arrangement" does not violate the requirements of the C language.

This means that, without violating the C language principles, the expression "A + = (a-= (A * A)" can be interpreted in a variety of ways to obtain non-unique results. This is called "ambiguity" or "ambiguity ".

The programming language and the program cannot have the ambiguity, which is completely different from the natural language we usually use.

To this end, C language special provisions:Between two adjacent vertices, the value saved in the same data object can be changed only once through the expression.The internal order of the expression "A + = (a-= (A * A)" is not ordered, so it must have the "two adjacent order points" in front and back ". However, in "A + = (a-= (A *, the sub-Effects of "+ =" and "-=" operations change the value of "a" of the same data object, this obviously violates the basic requirements of the C language for expression "the value saved in the same data object can be changed only once through expression evaluation". Therefore, this expression is an incorrect expression.

However, this error is different from a syntax error that violates the constraint of the C language. the compiler will acknowledge that such an expression does not violate the constraint of the C language and therefore can be compiled, this error may not be reported to the coder as a syntax error. At most, a good compiler may give a "warning"-It sniffed out the suspected error.

However, if you write an expression like "A + = (a-= (A * A)" in the Code, even though the compiler can continue to compile, however, the serious problem is that you do not know what expression you are writing, and the C language and compiler do not know either, the compiler will compile a program randomly based on its own "Understanding. C language calls this code behavior"Undefined behavior"(Undefined behavior ). Undefined behavior is an incorrect behavior. Only beginners will mistakenly think that the code is correct after compilation.

To give a more general example, the phrase "A book opposed to a wrong person's son" makes sense in syntax, but is ambiguous. Because it can be understood as a book opposed to mistaken children, or a book opposed to mistaken children ". In Chinese, this is a sentence, which is similar in C language. The C language does not specify whether "Books opposed to mistaken children" mean "Books opposed to mistaken children" or "Books opposed to mistaken children, this phenomenon is classified as "undefined behavior ".

 

To put it bluntly, such a huge bug turned out to be wrong with textbooks for more than 20 years. Today, it may be time to bury it.

To this end, I wrote this article to mourn it in advance. Not for mourning, it is for mourning.

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.