Details and examples of self addition and self subtraction operations in C language

  • 2020-05-19 05:24:53
  • OfStack

Since the increase since decreases

++ self-increment operators: such as a++, ++a are equivalent to a = a + 1; The self-subtraction operator: for example, the a, and the a are equivalent to a = a-1;

The difference between ++a and a++

Although ++a and a++ are equivalent to 1 result, the operation process is different. a++ first USES the value of a and then adds 1 to a, while ++a first adds 1 to a and then USES the value of a.

example


 #include <stdio.h> 
  int main() 
  { 
    //int m = 10, n1, n2; 
    //n1 = m++; First the m The value is assigned to n1 And then m Let's do the self-increment, so at this point, n1=10,m=11; 
    //n2 = ++m ; First the m You do the self-increment, and then you add the m Value is assigned to n2 So at this point, n2=11 . m=11 ;  

    int a = 10,b =10, c, d; 
    c = (a++) + (++a); 
    // The above example n1=m++,n2=++m,m++=11 Can be concluded that c=10+12; Analysis: the value of the preceding parentheses is 10 And that's in parentheses a After the self increment operation a The value of 11 , and assign it to the following parentheses a , in parentheses a After the self increment operation a The value of 12 , the final value of the parentheses is 12 ;  
    d = (++b) + (b++); 
    // The above example n1=m++,n2=++m,++m=11 Can be concluded that d=11+11; Analysis: the value of the preceding parentheses is 11 And that's in parentheses a After the self increment operation b The value of 11 , and assign it to the following parentheses b , in parentheses b Assign the value to the following parentheses first, so the value of the following parentheses is 11 ;  
    printf("c=%d\nd=%d\n",c,d); 
    return 0; 
  } 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: