C + + comma-expression

Source: Internet
Author: User

Transferred from: https://zhidao.baidu.com/question/106961628.html

The syntax for a comma expression is:
Expression 1, expression 2, ... Expression n
C + + order evaluates expression 1, expression 2, ..., value of expression N. For example:
int a,b,c;
A=l,b=a+2, c=b+3;
Because it is evaluated sequentially, it is guaranteed that B must be assigned after a, and C must be assigned after B. The comma expression can be represented by the following 3 ordered assignment statements:
A=1;
b=a+2;
c=b+3;
A comma expression is a value, which is not a substitute for a statement. The value of the comma expression is the value of the nth subexpression, which is the value of the expression N. For example:
int a,b,c,d;
D= (a=1,b=a+2,c=b+3);
cout<<d<<endl;
The output is:
6
The result of the output in the above example is the value of C.
A comma expression can also be used for parameters in a function call. For example:
Func (n, (j=1,j+4), k);
The function call has 3 arguments, and the middle argument is a comma expression. Parentheses are required, otherwise, the function has 4 parameters. The comma expression, as a value, can be used in almost all places.
In C + +, if the last expression of a comma expression is an lvalue, the comma expression is an lvalue. For example:
(a=1,b,c+1,d) =5;//ok: d=5
In C, a comma expression cannot be left-valued, so "(a=1,b,c+1,d) = 5;" will not compile

C + + comma-expression

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.