C language comma expression

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/hnrainll/archive/2011/08/03/2126660.html

The C language provides a special operator, the comma operator. Use it to connect two expressions. For example:


3 + 5, 6 + 8

It is also known as a comma expression and an ordered value operator ". The comma expression is generally in the form

Expression 1, expression 2

The process of solving a comma expression is: first solving expression 1, then solving expression 2. The value of the entire comma expression is the value of expression 2. For example, the value of the comma expression "3 + 5, 6 + 8" above is 14. Another example is the comma expression.
A = 3*5, a * 4
To solve this expression, the reader may have two different understandings: one thinks that "3*5, a * 4" is a comma expression. First, find the value of this comma expression, if the original value of A is 3, the value of the comma expression is 12, and 12 is assigned to a, so the last value of A is 12. "A = 3*5" is a value assignment expression, and "A * 4" is another expression. The two are connected by commas to form a comma expression. Which of the two is correct? The priority of the value assignment operator is higher than that of the comma operator. Therefore, a = 3*5 (that is, "A = 3*5" as an expression) should be solved first ). After calculation and assignment, the value of a is 15, and then a * 4 is obtained, and 60 is obtained. The value of the entire comma expression is 60.
A comma expression can form a new comma expression with another expression. For example, (A = 3*5, a * 4), a + 5 first calculates that the value of a is equal to 15, then perform the operation of a * 4 to 60 (but the value of A is not changed, and it is still 15), and then perform a + 5 to get 20, that is, the value of the entire expression is 20.
The general form of a comma expression can be extended

Expression 1, expression 2, expression 3 ...... Expression n

Its value is the value of expression n.

The comma operator is the lowest among all operators. Therefore, the following two expressions have different functions:

① X = (a = 3, 6*3)
② X = A = 3, 6 *

The first value is a value assignment expression, which assigns the value of a comma expression to X, and the value of X is equal to 18. The second is a comma expression, which includes a value assignment expression and an arithmetic expression. The value of X is 3.

In fact, the comma expression is nothing more than concatenating several expressions. In many cases, the purpose of using a comma expression is to obtain the values of each expression separately, rather than necessarily obtaining and using the values of the entire comma expression, comma expressions are most commonly used in loop statements (for statements.

Note that not all commas appear as comma operators. For example, function parameters are separated by commas. For example

Printf ("% d, % d, % d", A, B, C );

"A, B, c" in the previous line is not a comma expression. It is the three parameters of the printf function, and the parameters are separated by commas.
If it is changed

Printf ("% d, % d, % d", (a, B, c), B, c );

Then "(a, B, c)" is a comma expression whose value is equal to the value of C. The comma in the arc is not a delimiter between parameters, but a comma operator. The content in the arc is a whole and serves as a parameter of the printf function.
Strong expression ability in C language. One important aspect is its rich expression types and strong operator functions. Therefore, C is flexible and adaptable.

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.