Why does the expression a = a + b - ( b = a ) give a sequence point warning in c++?

后端 未结 4 605
萌比男神i
萌比男神i 2020-11-29 09:02

Following is the test code:

int main()
{
    int a = 3;
    int b = 4;
    a = a + b - (b = a); 

    cout << \"a :\" << a << \" \" <<         


        
4条回答
  •  有刺的猬
    2020-11-29 09:42

    To solve it separate them in two different statements.

    PS: Don't forget that humans may make mistakes performing arithmetic operations. Therefore is better to make the operations clearer by separating them in different statements. I hope I helped.

    int main() 
    {
       int a = 3;
       int b = 4;
    
       /* Two different Statements*/
       b = a;
    
       /* or a = a + b - a */
       a = a + b - b; 
    
       cout<<"a :"<

提交回复
热议问题