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

if p is a string contained in a string?

1384


How is a macro different from a function?

634


What is getch() function?

617


Why c is called procedure oriented language?

550


Differentiate between the = symbol and == symbol?

674






What are the application of c?

622


What is a 'null pointer assignment' error?

699


a way in which a pointer stores the address of a pointer which stores the value of the target value a) reference b) allocation c) multiple indirection d) none

604


Write a program to swap two numbers without using the third variable?

579


Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?

720


What is the equivalent code of the following statement in WHILE LOOP format?

718


i have a written test for microland please give me test pattern

2149


What should malloc() do?

621


Hi can anyone tell what is a start up code?

1594


Find the second largest element in an array with minimum no of comparisons and give the minimum no of comparisons needed on an array of size N to do the same.

683