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 :
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.
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.
Q.1 Solve the expression in C language compiler ,assuming x,z are integer variables:
z = x++ + x++
where x=7
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
//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
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>
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>
3 comments:
Nice post i love it Job Interview questions and answers
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.
It's really helped me a lot, finally I am able to solve it ��
Post a Comment
Dont Forget for Commets