Comma expressions in the C language

Source: Internet
Author: User

Reprint: 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 together. Such as:

3+5,6+8

Called a comma expression, also known as the order evaluation operator. The general form of a comma expression is

Expression 1, Expression 2

The solution to the comma expression is to solve the expression 1 first and then solve the expression 2. The value of the entire comma expression is the value of expression 2. For example, the above comma expression "3+5,6+8" has a value of 14. As another example, a comma-expression
A=3*5,a*4
To solve this expression, the reader may have two different understandings: one thinks "3*5,a*4" is a comma expression, the value of this comma expression is first calculated, if the original value of a is 3, then the value of the comma expression is 12, assigns 12 to a, so the last value of a is 12. Another thought: "A=3*5" is an assignment expression "," a*4 "is another expression, the two are concatenated with commas to form a comma expression. Which of the two is right? The assignment operator has a higher precedence than the comma operator, so a=3*5 should be solved first (that is, "a=3*5" as an expression). After calculation and assignment, the value of a is 15, and then the a*4 is solved by 60. The value of the entire comma expression is 60.
A comma expression can also form a new comma expression with another expression, such as (A=3*5,A*4), a+5 first calculates that the value of a is equal to 15, then the operation of A*4 is 60 (but the value of a is not changed, still 15), and then A+5 20, that is, the value of the entire expression is 20.
The general form of a comma expression can be extended to

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

Its value is the value of the expression N.

The comma operator is the lowest level in all operators. Therefore, the following two expressions function differently:

①x= (a=3,6*3)
②x=a=3,6*a

① is an assignment expression that assigns the value of a comma expression to the value of x,x equal to 18. The ② is a comma expression that includes an assignment expression and an arithmetic expression, with the value of x 3.

In fact, a comma expression is nothing more than "concatenating" several expressions together. In many cases, the purpose of using a comma expression is simply to get the values of each expression separately, rather than necessarily getting and using the entire comma-expression value, the comma expression is most commonly used in a loop statement (for statement).

Note that not all occurrences of commas are used as comma operators. For example, function parameters are separated by commas. Such as

printf ("%d,%d,%d", a,b,c);

The "A,b,c" in the previous line is not a comma expression, it is 3 parameters of the printf function, with a comma interval between the parameters.
If you overwrite the

printf ("%d,%d,%d", (a,b,c), b,c);

Then "(A,b,c)" is a comma-expression with a value equal to the value of C. The comma in parentheses is not a delimiter between parameters but a comma operator. The content in parentheses is a whole, as a parameter to the printf function.
C Language Expression Ability is strong, one of the important aspect is that its expression type is rich, the operator function is strong, therefore C uses flexibly, the adaptability is strong

Comma expressions in the C language

Related Article

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.