**Increment operators (++)**

- Increment operators are increase the value of subsequent. value may be increase according to the
- Increment operator are two types as follows :
- Post increment
- 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 :

- Post decrement
- 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 :

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.

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

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**-**8**-**(**-**1)**-**8
z= 8

**-**1**+**1**-**8**-**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

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

*hint:***Q.4**solve the following equations:

z = x++

*****y++

**/**++x

**-**--y

**%**x++

where x = 5 and

y = 2

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

*hint:*--------------------------------------------------------------------------------

**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

**-**x

**+**--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.

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

ReplyDeletegood explanantion

ReplyDeletegr88888888888

ReplyDeleteplz explain ans of ques 3?i thnk z should cme 17

ReplyDeleteNo, z will be 18.

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

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

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?

DeleteYour welcome!

DeleteI happy to solve your question, so carry on and ask your question.

int x,y,z;

Deletex=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?

The above program output would be:

Deletex=2 y=1 z=1

for reference, you can check this program:

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

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

Delete@Navdeep,

Deleteyour above programs explanations at:

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

thank u so much dinesh....

Deleteint x=1;

Deleteint y= x++ - ++x + ++x - x--;

value of y and x

main()

ReplyDelete{

int n=2;

printf("%d %d\n", ++n, n*n);

what will be the output?

The output of above program would be: 3 4

DeleteBecause 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

y does the memory value of n remain constant??

DeleteValue 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

Delete3 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,

Deletea=4,b=3

ReplyDeletec=++a + b++ + --a + b--

The output of above program would be: a=4 b=3 c=14

Deletec = ++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

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

ReplyDeletevoid main()

{

int i=5;

printf("%d",i++ + ++i);

}

@HAppy dAys,

Deletei = 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

main()

ReplyDelete{

int i=0;

for(;i++;printf("%d",i)) ;

printf("%d",i);

}

@HAppy dAys,

DeleteIt 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.

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

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

ReplyDeleteThank u sir ji

ReplyDeleteint=4;

ReplyDeletefor(i>6;i--;i>5)

printf("hello:%d",i);

can anyone tel me what is 'i' ?

hello:3hello:2hello:hello:0

DeleteQ-Can you tell me the output with explanation??

ReplyDeletevoid 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();

}

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

DeleteAll 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?

Deletedifference 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 ! !

DeleteThat'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.

DeleteThe 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..

plz give explanation of que. 4

ReplyDeleteNoooo... m getting z=19 for Question no. 3.. :( please help asap..

ReplyDeletemain()

ReplyDelete{

int i=10;

print(" %d %d %d",++i,i++,++i);

}

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.

DeleteI am not able to get the solution of Question 2

ReplyDeletez=++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

Could you help me with this problem???

ReplyDeletevoid main()

{

int a=3;

printf("%d %d %d",a++,a,++a);

}

@rajat singh

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

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

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

ReplyDelete#include

int main()

{

int a=3,z;

z= ++a + ++a + ++a;

printf("%d, %d",z,a);

return 0;

}

I am not sure about this .......but i consider this logic

Deletez=++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.*/

plz provide the ans:

ReplyDelete#include

void main()

{

int i=5;

printf("%d", i++ + ++i + i-- );

getch();

}

Explain Qus 4 :

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

where x = 5 and

y = 2

Value of x = 0,y=2 and x=8

Deletein Q-2 z=-15.please check

ReplyDeleteHi please any one clarify me on the

ReplyDelete1*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............

#include

ReplyDeletemain()

{int i=10;

printf("%d",++i + ++i + ++i );

}

bhai ur way of justification is shown wrong by devcpp compiler

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

ReplyDelete/* 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

{

ReplyDeleteint x=10;

printf("\n%d",++x);

printf("\n%d",x++);

printf("\n%d",x);

}

what will be the anser and how please explain sir.

is it compiler dependent???????????????????

ReplyDeletecan any1 explain the following program with the output.. please..?

ReplyDelete#include

#include

int main()

{

int i=5;

printf("%d%d",++i,i--,i,i++,i--);

getch();

}

please tell me the ans of this program

ReplyDeleteint x=10,y,z;

z=y=x;

y-=--x;

z-=x--;

x-=--x-x--;

x=6 y = 0 and z =2

Deleteint x=10;

ReplyDeletex=x++ + ++x+ x++;

printf("%d",x);

please explain

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

DeleteConsider the following statements,

ReplyDeleteint 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??

why we get diff ans in java script

ReplyDeletez = 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

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#include

ReplyDeleteint 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

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

ReplyDeleteSolve the following expression:

ReplyDeletez = ++x + y-- - ++y - x-- - x-- - ++y - x--

Where x = 7

y = -3

how to do this?????