C, C + + difference left value right value

Source: Internet
Author: User

Tag: C + + syntax c differences

C and C + + have some differences in syntax details, although these differences may not be enough to cause differences in the results, some code does have an impact.


This time, the main summary is the difference of the left value right value.

In C, the result of many lvalue operators is no longer an lvalue, whereas in C + +, the result of the lvalue operator is still an lvalue, as long as it is logically feasible. This way of C + + allows for greater flexibility between operator expressions.


1, ++i, we are accustomed to in C + +, I self-added back to myself; however, in c I self-added, the return is a temporary copy, that is, and i++ is the same, this result cannot be an lvalue, that is (++i) = 0 illegal. So I think that's why i++ is used in many for loops in the code, because in C I think i++ is the same as ++i's efficiency.

2, =, similarly, the result in C is the right value, i.e. (a = b) = c is not valid in C, but it is possible in C + +.

3,?:, the branch result returned in C is an rvalue, as stated in C + +, as long as the two branches are lvalue and of the same type, the result is also Lvalue (see, C + + programming language-Special edition section 6.2). This is the following:

{       (1&NBSP;?&NBSP;I=11&NBSP;:&NBSP;J) =22; // c++ The result is i =  22, illegal    printf in C ("%d %d \n",  i, j);       1  ? i=11 : j=22; // c++ result is i = 11, illegal in C, the correct wording is:  1 ? i=11  :  (j=22);    printf ("%d %d \n",  i, j);   //  Thought the result is the same as above, because  ?: Priority is higher than  =, supposedly equivalent to the above expression    //  However, this is the reality, things will never follow your thoughts. The syntax rules in    // c++ are determined   equivalent to:   //  (1)  ?  (i=11)  :  (j =22);   //  See also the C + + programming language-Special edition   section 6.2, such a wonderful expression:   // a =  b < c ? d = e : f = g;  equivalent to:    //  a =  (  (b < c)  ?  (d = e)  :  (f = g)  );   //  This isis the so-called grammar. But it's also quite in line with the programmer's idea. }





This article is from the "V" blog, be sure to keep this source http://4651077.blog.51cto.com/4641077/1613799

C, C + + difference left value right value

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.