9.09.2011

Increment and Decrement operators

Increment operators (++)

  • Increment operators are increase the value of subsequent. value may be increase according to the programmer.
  • Increment operator are two types as follows :
    1. Post increment
    2. Pre increment
 Decrement operators ( -- )
  • decrement operators decrease the value to one, two and so on.
  • As like Increment operators, decrement operators are also two type as : 
    1. Post decrement
    2. Pre decrement
Before we more discuss about increment and decrement operator, let understand some expression; that's are using in all operators.

Now some issues of pre and post increment and decrement, we will discuss after solve following programming :

 x = 5       x = 5     x = 5  
 x = x+1      ++x       x++    
 x = 6       x = 6      x=5       

In above program u notice that value of x increase to one in all three expression but after the increasing one, the final value of x in not same, third box result is differ to another two boxes. Now you know that c operators are mostly three types, ++ and -- operators are Unary operators.

Post increment or decrement are lowest precedence so it is solve at the end of program. Hence above program in third box first value of x is print i.e. 5 after, it is increase.

Unary operators are solve right to left.


Q.1 Solve the expression in C language compiler ,assuming x,z are integer variables:
z = x++ + x++
where x=7
Ans.
stepwise evaluation of this expression is shown below :

 z = x++ + x++ + ++x
 //post increment of x=2
 z = x + x + ++x
 //++x increase the value of x one so x=8 
 /*Now all pre and post increment/decrement are  solved so we write the the expression in normal mathematical expression*/
 z = x + x + x
 //put the current value of x i.e. 8
 z = 8 + 8 8
 z = 24


syntax : x=current value-post increment/decrement
so x = 8 + 2
   x = 10

Q.2 Solve the following expression : 
z = ++x + y-- - ++y - x-- - x-- - ++y - x--
where   x = 7
        y = -3
Ans.
 z=++x  + y-- - ++y - x-- - x-- - ++y - x--
 /* post decrement of x=3 and
    post decrement of y=1 */
 z= ++x + y - ++y - x - x - ++y - x
 /* its unary operator so we solve it from
 right to left*/
 z=++x + y - ++y - x - x - y - x
 //now y=-3+1=-2
 z=++x + y - y - x - x - y - x
 //now y=-2+1=-1
 z=x + y - y - x - x - y - x
 //now x=7+1=8
 /*now all increment and decrement operators  
 all solved so we put the current value of x  
 and y.*/
 z= 8 + (-1) - (-1) -- 8 - (-1) - 8
 z= 8 + 1 - 8 + 1 - 8
 z= - 16 + 1
 z= -15

 x=8-3=5  
 y=-1-1=-2


Related some increment or decrement operators exercise:

Q.3  solve the following equations:

z = x++ + ++y - x-- + --y

where x = 7 and 
         y = 9

hint:(For your cross-checking, answer is x=7, y=9 and z=18).

Q.4 solve the following equations:

z = x++ * y++ / ++x - --y  % x++

where x = 5 and
         y = 2

hint:(For your cross-checking, answer is x=8, y=2 and z=0).

--------------------------------------------------------------------------------

Q.3 answer with explanation :

z = x++ + ++y - x-- + --y


where x = 7 and 
         y = 9

Ans:

post decrement of x = 1
post increment of x = 1

z = x + ++y - + --y
z = x + ++y - x + y  // now y = 9-1 = 8
z = x + y - x + y      // now y = 8+1 = 9
// now put the value of x and y
z = 7 + 9 - 7 + 9
z = 9 + 9
z = 18

Hence, our final answer will be:

x = 7 - 1 + 1 = 7
y = 9
z = 18


Related program:

What will be output of below program:

int i=1;
printf("%d%d%d",i,++i,i++);

Click here for answer.

64 comments:

  1. faad sir ji awsome...................

    ReplyDelete
  2. plz explain ans of ques 3?i thnk z should cme 17

    ReplyDelete
    Replies
    1. No, z will be 18.

      The explanation of Q.3 i add above for easily readable.

      Check your answer, and if any trouble comment me right now.

      Delete
    2. i got it thnx a lot....actually i wsadding previous value of y i.e y i ws getting wrng answer...cn i ask u another question?

      Delete
    3. Your welcome!
      I happy to solve your question, so carry on and ask your question.

      Delete
    4. int x,y,z;
      x=y=z=1;
      z=++x||++y&&++z;
      printf("\n x=%d y=%d z=%d \n",x,y,z);


      plz tell me the output?

      acc to me ans is x=2,y=2 and z=1...is it right?

      Delete
    5. The above program output would be:
      x=2 y=1 z=1

      for reference, you can check this program:

      http://www.cprogrammingcodes.blogspot.in/p/c-quiz.html

      Delete
    6. i got the value of x and z but am not getting hw y is cmng 1?cn u plz explain?

      Delete
    7. @Navdeep,

      your above programs explanations at:

      http://cprogrammingcodes.blogspot.in/2012/09/short-circuit-operators.html

      Delete
    8. thank u so much dinesh....

      Delete
    9. int x=1;
      int y= x++ - ++x + ++x - x--;

      value of y and x

      Delete
  3. main()
    {
    int n=2;
    printf("%d %d\n", ++n, n*n);

    what will be the output?

    ReplyDelete
    Replies
    1. The output of above program would be: 3 4

      Because when compiler reach ++n, its increase n to +1 i.e. n=3
      but in memory value of n remain constant so n*n = 2*2 =4

      Delete
    2. y does the memory value of n remain constant??

      Delete
    3. Value of n doesn't remain constant. The function arguements are always passed from right to left into the stack. As printf() is a builtin function its been passed into the stack from right to left and so n*n is evaluated first as 4 and n value remains the same 2 at this point as there is no modification in n value. now ++n changes the n value to 3. NOTE: The printf() is evaluated from right to left but the values are printed in the same order which is given, hence the op is 3 4

      Delete
    4. 3 4 bcoz inside a pf fun operation takes place frm right to left,so n*n will be executed 1st,den ++n will giv d result 3 becoz bfor n value was 2,

      Delete
  4. a=4,b=3
    c=++a + b++ + --a + b--

    ReplyDelete
    Replies
    1. The output of above program would be: a=4 b=3 c=14

      c = ++a + b++ + --a + b--
      //post decrement of b = 1
      //post increment of b = 1
      c = ++a + b + --a + b
      c = ++a + b + a + b // a = 4-1 = 3
      c = a + b + a + b // a = 3+1 = 4
      c = 4 + 3 + 4 +3 // putting value of a and b
      c = 14

      Hence, value of a = 4
      value of b = 3 - 1 + 1 = 3
      value of c =14

      Delete
  5. CAn you please explain,what is the output for this program??

    void main()
    {
    int i=5;
    printf("%d",i++ + ++i);
    }

    ReplyDelete
    Replies
    1. @HAppy dAys,

      i = 5
      //for explanation we can write i++ + ++i is equal to z
      z = i++ + ++i
      //post increment of i = 1
      z = i + ++i
      z = i + i //i = 5+1 = 6
      z = 6 + 6 //putting the current value of i
      z = 12

      Hence, the output would be 12.

      But value of i increase to one because there are one post increment available so,
      i = 6 + 1
      i = 7

      Delete
  6. main()
    {
    int i=0;
    for(;i++;printf("%d",i)) ;
    printf("%d",i);
    }

    ReplyDelete
    Replies
    1. @HAppy dAys,

      It is simple, we can write above for loop as:
      for(i=0; i++; printf("%d",i)); // or
      for(i=0; i++; z); // or
      for(i=0; i++; ); // or

      All is equal, what is this mean? i.e. this is an infinite loop but it(loop) is not have any Increment/Decrement so its terminate and goes to next line of program.

      i is post increment so after terminate of loop i's value increase to one i.e. i = 0+1 = 1.
      Hence, output of above program would be 1.

      The for loop concept:
      for(initialize; conditions; increments/decrements)
      {
      statement
      }

      note: define z as int.

      Delete
    2. this is a wrong explaination since i++ returns 0 i.e initial value of i and hence body of loop is not executed

      Delete
  7. can u plzz provide an explanation for Q.4.....if we use bodmas rule then i am getig an ans of -1

    ReplyDelete
  8. int=4;
    for(i>6;i--;i>5)
    printf("hello:%d",i);


    can anyone tel me what is 'i' ?

    ReplyDelete
  9. Q-Can you tell me the output with explanation??

    void main()
    {
    clrscr();
    int j,l,x=3;
    j=x++ + x++ + ++x + ++x;
    l=++x + ++x + x++ + x++;
    printf("j=%d\nl=%d",j,l);
    getch();
    }

    ReplyDelete
    Replies
    1. x is pre incremented twice in the expression which solves the value of j, so x becomes x+2 that is 5(which is to be substituted in soving j)...therefore j=5+5+5+5=20...and after terminating the expression of solving j, the value of x will be x+4=7....same in the case of l also

      Delete
    2. All these answers are related to turbo c right? What will be the outputs in case of gcc compiler and which answers does the companies expect?

      Delete
    3. difference between turbo C and GCC compiler is that, the memory spaces differ for basic primitives like int,float etc...but coming to the above program, the output will be the same with respect to both the compilers, as size of the variables stored is not the basic thing...we just need to know the final value right ! !

      Delete
    4. That's not the right case. There are certain difference in evaluation of variables involving side effects. For example the above program has side effects which shows the difference between the gcc compilers and turbo c.
      The output of the above program with respect to gcc is
      j=15
      l=36
      and with respect to turbo c is
      j=20
      l=36
      Hope you have an answer for this..

      Delete
  10. plz give explanation of que. 4

    ReplyDelete
  11. Noooo... m getting z=19 for Question no. 3.. :( please help asap..

    ReplyDelete
  12. main()
    {
    int i=10;
    print(" %d %d %d",++i,i++,++i);
    }

    ReplyDelete
    Replies
    1. printf() will work with the help of stack i.e. LIFO approach. when we are working with printf() always required to pass toward from left to right and data required to print towards from left to right.so first of all we take last i value i.e. pre inc. oprator so value inc. by 1 i.e. 11 and store in to the stack.then R->L 1 dec. operator so first store the value in stack i.e. 11 and then inc. by 1 i.e. 12. now fromR->L i more pre operator is available so first value inc. by 1 and i.e. 13and store in the stack. now print the value from L->R i.e. 13 11 11. ans.

      Delete
  13. I am not able to get the solution of Question 2
    z=++x + y-- - ++y - x-- - x-- - ++y - x--
    /* post decrement of x=3 and
    post decrement of y=1 */
    z= ++x + y - ++y - x - x - ++y - x
    /* its unary operator so we solve it from
    right to left*/
    z=++x + y - ++y - x - x - y - x

    STARITNG FROM END HOW ++y WAS WRITTEN AS SIMPLE y

    ReplyDelete
  14. Could you help me with this problem???
    void main()
    {
    int a=3;
    printf("%d %d %d",a++,a,++a);
    }

    ReplyDelete
    Replies
    1. @rajat singh

      Your above C program problem - solution with explanation:Click here

      Delete
    2. I'm sorry but the link to show answer on the next page isn't working....

      Delete
  15. Im expecting the output as 18, 6 but unfortunately the output is 16, 6. how is it so ?

    #include
    int main()
    {
    int a=3,z;
    z= ++a + ++a + ++a;
    printf("%d, %d",z,a);
    return 0;
    }

    ReplyDelete
    Replies
    1. I am not sure about this .......but i consider this logic

      z=++a + ++a + ++a ; // which means
      z= 4+ ++a + ++a ;
      /*considering (a=a+1) which is 4 ,the value of second and third a=4 (third a incremented by 1 time by first operation */
      z=4+ a+1 + ++(a+1);
      z= 4 +5 + ++(5+1) ;
      /* where a =5 now and now again third a is incremented another time by the influence of second operation.*/
      z=4+5+ 7 = 16 ;
      /*and finally the a has become 6 i.e 5+1 from above last but one exp.*/

      Delete
  16. plz provide the ans:

    #include
    void main()
    {
    int i=5;
    printf("%d", i++ + ++i + i-- );
    getch();
    }

    ReplyDelete
  17. Explain Qus 4 :

    z = x++ * y++ / ++x - --y % x++

    where x = 5 and

    y = 2

    ReplyDelete
  18. Hi please any one clarify me on the

    1*2/4^4%5/6*2 = ????????

    how the execution will be?

    how it will be calculated ?

    please give any formula for it?

    what are the priorities for it?


    please clarify me on this............

    ReplyDelete
  19. #include


    main()
    {int i=10;
    printf("%d",++i + ++i + ++i );

    }

    bhai ur way of justification is shown wrong by devcpp compiler

    ReplyDelete
  20. z=++x + y-- - ++y - x-- - x-- - ++y - x--
    /* post decrement of x=3 and
    post decrement of y=1 */
    z= ++x + y - ++y - x - x - ++y - x
    /* its unary operator so we solve it from
    right to left*/
    z=++x + y - ++y - x - x - y - x
    //now y=-3+1=-2
    z=++x + y - y - x - x - y - x
    //now y=-2+1=-1
    z=x + y - y - x - x - y - x
    //now x=7+1=8
    /*now all increment and decrement operators
    all solved so we put the current value of x
    and y.*/
    z= 8 + (-1) - (-1) -8 - 8 - (-1) - 8
    z= 8 - 1 + 1 - 8 - 8 + 1 - 8
    z= - 16 + 1
    z= -15 but output shown by compiler for same program is -16 by devcpp compiler

    ReplyDelete
  21. {
    int x=10;
    printf("\n%d",++x);
    printf("\n%d",x++);
    printf("\n%d",x);
    }
    what will be the anser and how please explain sir.

    ReplyDelete
  22. is it compiler dependent???????????????????

    ReplyDelete
  23. can any1 explain the following program with the output.. please..?
    #include
    #include

    int main()
    {
    int i=5;
    printf("%d%d",++i,i--,i,i++,i--);
    getch();
    }

    ReplyDelete
  24. please tell me the ans of this program
    int x=10,y,z;
    z=y=x;
    y-=--x;
    z-=x--;
    x-=--x-x--;

    ReplyDelete
  25. int x=10;
    x=x++ + ++x+ x++;
    printf("%d",x);
    please explain

    ReplyDelete
    Replies
    1. x = 35, expression is start to evaluate from right to left, so value of x is 11 in second statment, so total is 33 and two ++ is still remaining so 35

      Delete
  26. Consider the following statements,
    int i=4, j=3, k=0;
    k=++i - --j + i++ ---j +j++; What will be the values of i, j and k after
    the statement.
    (A) 7, 2, 8 (B) 5, 2, 10
    (C) 6, 2, 8 (D) 4, 2, 8
    What is the answer and why??

    ReplyDelete
  27. why we get diff ans in java script
    z = x++ + x++ + ++x
    I got
    z=25 in java script

    even
    int i=1;
    printf("%d%d%d",i,++i,i++);

    This is giving
    Ans : 122

    why so

    ReplyDelete
    Replies
    1. at first the value of i is 1 hence it displays 1 ,then ++i value of i (i.e)1 is incremented it becomes 2 and it is stored in the variable i(now,i=2) and for i++ the value of i which is 2 is displayed and it gets incremented

      Delete
  28. #include

    int main(void) {
    int i=1;
    printf("%d%d%d%d",++i,--i,i++,i--);
    return 0;
    }

    the above program output is 1101 .how it is?
    can anyone explain it

    ReplyDelete
  29. your answer for Question 1 is incorrect. The actual output will be -11.

    ReplyDelete
  30. Solve the following expression:
    z = ++x + y-- - ++y - x-- - x-- - ++y - x--
    Where x = 7
    y = -3
    how to do this?????

    ReplyDelete