Click here to Skip to main content
15,890,982 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
XML
#include <stdio.h>
#include <conio.h>
void main()
{

int x=2,y=3;
x=++y + ++x;
y=++y + ++x;
printf("%d%d",x,y);
getch();




}



The output of code is 8 and 13



i want how this comes plz help i
Posted

First study about the pre-increment and post-increment operators then you will know exactly what happening.

pre-increment will be done before execution of statement
and post-increment will be done after execution of statement

so
your result will be as below.

x=++y + ++x;

here
 ++y=4,++x=3
x=4+3=7 


y=++y + ++x;


from above statement
++y=4+1=5
++x=7+1=8
y=5+8=13

so finally x=8 and y=13
 
Share this answer
 
v2
x=++y + ++x;

means
x=(1+3)+(1+2)=7 ,y=4

y=++y + ++x;

means
y=(1+4)+(1+7)=13
 
Share this answer
 
v3
Follow the guideline given in your last question and you can figure this out.
 
Share this answer
 
Comments
Albert Holguin 20-Oct-11 10:41am    
This isn't a solution, post as a comment in the future.
Mehdi Gholam 20-Oct-11 10:50am    
This is the second question from the OP in this regard.
Albert Holguin 20-Oct-11 15:46pm    
It's still not a solution... flag repeat questions as "repost"
As you probably know, there are two kinds of increment (and decrement) operators. One comes before the operand - and is called prefix (++x), and the other one comes after the operand - and is called postfix (x++). Same can be said about the -- (decrement) operators.

Lets see what happens when we're applying both operators
C++
int x = 5;
int y = 5;

printf("x = %d, y = %d",++x, y++);


The answer would be: "x = 6, y = 5". Why is that?
The reason is that the prefix operator (that in this case is being applied to x), is incrementing the value of x by one, and uses the result right away. In contrary, y's current value is being used, and only afterwards increments. Look at an equivalent example:

C++
int x = 5;
int y = 5;

x = x + 1;
printf("x = %d, y = %d",x,y);
y = y + 1;


So lets translate now the example that you've presented, into a similar code.

C++
int x=2,y=3;
x=++y + ++x;
y=++y + ++x;
printf("%d %d",x,y);


becomes:
C++
int x=2,y=3;

y = y+1; // now y = 4
x = x+1; // now x = 3
x = y + x; // now x = 4 + 3 = 7

y = y+1; // now y = 5
x = x+1; // now x = 8
y = y + x; // now y = 5 + 8 = 13

printf("%d %d",x ,y); // output is 8 and 13.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900