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
#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); }
Differentiate between new and malloc(), delete and free() ?
Differentiate between #include<...> and #include '...'
Explain null pointer.
List the different types of c tokens?
Write a program to reverse a given number in c?
How main function is called in c?
What is integer constants?
Do string constants represent numerical values?
Differentiate call by value and call by reference?
What are the advantages of using new operator as compared to the function malloc ()?
What is a void pointer? When is a void pointer used?
find out largest elemant of diagonalmatrix
What are the applications of c language?
Explain a pre-processor and its advantages.