Pre increment vs Post increment in array

前端 未结 3 1060
陌清茗
陌清茗 2020-11-29 01:39

I am learning programming and I have started from C language. I was reading Let us C book. And I was going through this program in that book.

main( ) 
{ 
 in         


        
相关标签:
3条回答
  • 2020-11-29 02:04

    Your understanding is not exactly correct. Pre-increment and post-increment operators are unary operators.

    So, initially if b = 5, then ++b or b++ increments the value of b to 6. However, the difference between pre and post comes when you are using an assignment operator "=".

    So,

    if b=5
    a=b++ // after this statement a=5 and b=6 as it is post increment
    c=++b // after this statement c=7 and b=7
    

    For clear understanding, you can divide the above statements as:

    a=b;  
    b=b+1; //post increment  
    b=b+1; //pre increment  
    c=b;`  
    

    So, the example you gave:

    main( )    
    {      
     int a[5] = { 5, 1, 15, 20, 25 } ;     
     int i, j, k = 1, m ;  
     i = ++a[1] ; // a[1] = 2 and i = 2  
     j = a[1]++ ; // j = 2 and a[1] = 3  
     m = a[i++] ; // m = a[2++] = 15, i now becomes 3  
     printf ( "\n%d %d %d", i, j, m ) ; // so i =3, j= 2 and m =15  
    }
    

    For clarity, I am splitting the above code into multiple statements:

    main( )    
    {      
     int a[5] = { 5, 1, 15, 20, 25 } ;     
     int i, j, k = 1, m ;
     a[1] = a[1] + 1;  
     i = a[1];  
     j = a[1];  
     a[1] = a[1] + 1;  
     m = a[i]; // m = a[2] = 15  
     i = i + 1;  
     printf ( "\n%d %d %d", i, j, m ) ; // so i =3, j= 2 and m =15  
    }
    

    I hope the above explanation clears your doubt and the output of the program you are running.

    0 讨论(0)
  • 2020-11-29 02:17

    You hit the nail on the head. Your understanding is correct. The difference between pre and post increment expressions is just like it sounds. Pre-incrementation means the variable is incremented before the expression is set or evaluated. Post-incrementation means the expression is set or evaluated, and then the variable is altered. It's easy to think of it as a two step process.

    b = x++;
    

    is really:

    b = x;
    x++;
    

    and

    b = ++x;
    

    is really:

    x++;
    b = x;
    

    EDIT: The tricky part of the examples you provided (which probably threw you off) is that there's a huge difference between an array index, and its value.

    i = ++a[1];
    

    That means increment the value stored at a[1], and then set it to the variable i.

    m = a[i++];
    

    This one means set m to the value of a[i], then increment i. The difference between the two is a pretty big distinction and can get confusing at first.

    Second EDIT: breakdown of the code

    { 
     int a[5] = { 5, 1, 15, 20, 25 } ; 
     int i, j, k = 1, m ; 
     i = ++a[1] ; 
     j = a[1]++ ; 
     m = a[i++] ; 
     printf ( "\n%d %d %d", i, j, m ) ; 
    }
    

    First:

    i = ++a[1];
    

    At this point we know a[1] = 1 (remember arrays are zero indexed). But we increment it first. Therefore i = 2.

    j = a[1]++;
    

    Remember we incremented a[1] before, so it is currently 2. We set j = 2, and THEN incremented it to 3. So j = 2 and now a[1] = 3.

    m = a[i++];
    

    We know i = 2. So we need to set m = a[2], and then increment i. At the end of this expression, m = 15, and i = 3.

    In summary,

    i = 3, j = 2, m = 15.
    
    0 讨论(0)
  • 2020-11-29 02:28

    Explanation:

    Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .

    Step 2: int i, j, m; The variable i,j,m are declared as an integer type.

    Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

    Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

    Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

    Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m

    Hence the output of the program is 3, 2, 15

    0 讨论(0)
提交回复
热议问题