main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}

Answer Posted / joseph

Here this should make it easier for you...

Problem_________|___Solution__|

int x=20, y=35; (here the values of x,y are apparent.)

x = y++ + x++; (x=y+x+1) or(x = 35 + 20 + 1)x = 56
But; you incremented y, its now = 36

y = ++y + ++x; (y =(y+1)+(x+1)) or(y=1+36+1+56)y = 94
This is the second time you incremented
x so it is now = 57.


The reason that you are getting different increases
for x and y is that when you use statement(x=x++) you are
first stating that x is = to x, and then 'increment x.
when you use statemnt(x=++x) you are first
stating 'increment x, then that x is = to x.

look at the code and description in the chart below.

table:
code = meaning;
int x=2
int y=2
-------------------|
(x=x++) = "x = x, x + 1" (increment happens after)
(x=++x) = "x = (x+1)" (increment happens before)
(x=y++) = "x = y, y + 1" (increment happens after)
(x=++y) = "x = (y+1)" (increment happens before)

if you want to add y to x and then increment y use this
statement:

x+=y++

if you want to increment y and then add it to x use this
statement:

x+=++y

Is This Answer Correct ?    25 Yes 7 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

#define MAX(x,y) (x) >(y)?(x):(y) main() { inti=10,j=5,k=0; k= MAX(i++,++j); printf("%d..%d..%d",i,j,k); }

784


Differentiate between new and malloc(), delete and free() ?

676


Differentiate between #include<...> and #include '...'

618


Explain null pointer.

623


List the different types of c tokens?

629






Write a program to reverse a given number in c?

602


How main function is called in c?

632


What is integer constants?

625


Do string constants represent numerical values?

926


Differentiate call by value and call by reference?

570


What are the advantages of using new operator as compared to the function malloc ()?

759


What is a void pointer? When is a void pointer used?

627


find out largest elemant of diagonalmatrix

1651


What are the applications of c language?

627


Explain a pre-processor and its advantages.

634