Wednesday

Increment or Decrement operators


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.

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 - 8 - (-1) - 8
 z= 8 - 1 + 1 - 8 - 8 + 1 - 8
 z= - 16 + 1
 z= -15
 x=8-3=5  
 y=-1-1=-2

For interview questions and answers visit  <a href="http://www.online24by7.info">Interview questions and </a>


Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

3 comments:

Unknown 26 March 2013 at 19:40 said...

Nice post i love it Job Interview questions and answers

Arka Bandyopadhyay 8 December 2015 at 17:02 said...

Sorry , but 24 isnot the right answer for the first problem i have code it and run it in turbo c and gcc compiler it's giving 28 and 23 answer.

Anonymous said...

It's really helped me a lot, finally I am able to solve it ��

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |