Comma and comma expressions in the C language

Source: Internet
Author: User

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-expressionA=3*5,a*4To 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.
printf ("%d,%d,%d", a,b,c);
C language expressive ability, one of the important aspects is that its expression type is rich, the operator is strong, so C use flexible, strong adaptability

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.