ChetanaSforum

c aptitude plz explain

Recommended Posts

What will be printed as the result of the operation below:main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(“%d%dn”,x,y);}

Share on other sites

What will be printed as the result of the operation below:main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(“%d%dn”,x,y);}

I think x=55, y=57

Share on other sites

its answer is 5794...but i don't know the reason....

Share on other sites

it is 57 & 94pretty straight forward.Dont increment x in first line as it is ++ operator. then x = 55, but after that line value of x is incremented as postfix operator was there in first line so now x=56 and same with y ie y = 36now in next line prefix operator & thus x and y will be incremented before they are usedx= 57 & y = 57+37=94:)) Cheers, Practice & enjoy

Share on other sites

What will be printed as the result of the operation below:main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(“%d%dn”,x,y);}

Share on other sites

it is 57 & 94pretty straight forward.Dont increment x in first line as it is ++ operator. then x = 55, but after that line value of x is incremented as postfix operator was there in first line so now x=56 and same with y ie y = 36now in next line prefix operator & thus x and y will be incremented before they are usedx= 57 & y = 57+37=94:)) Cheers, Practice & enjoy

ya he s corect but ther s no space betwen them bcos ther s no space or new line operator b/w %d's

Share on other sites

ya he s corect but ther s no space betwen them bcos ther s no space or new line operator b/w %d's

ans:-5794

Share on other sites

What will be printed as the result of the operation below:main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(“%d%dn”,x,y);}

The answer is 57 94I explain you clearly don't be worried...First know that x++ will increment the value by one but the value didn't stored in x...and at the same time ++x increments the value of x and stores the new x's value in x...so inint x=20,y=35;x=y++ + x++; here we get y++=35++=36 but still y=35 only not 36 because it is y++, but this 36 value is kept in memory until the compiler moves to next line but still y=35 only...But due to keeping 36 in memory that value is taken and added with x++,since we know x++=20++=21,this value also kept in memory and still x=20 onlynow x=36+21 gives x=57 but still y=35 onlybuty=++y=++35 gives y=36 now only and then ++x=++57 gives x=58so now y=36+58=94now the result isx=57y=94I think so this explanation will be useful to u friends ----Regards S.Sathiyamoorthy.

Share on other sites

The answer is 57 94I explain you clearly don't be worried...First know that x++ will increment the value by one but the value didn't stored in x...and at the same time ++x increments the value of x and stores the new x's value in x...so inint x=20,y=35;x=y++ + x++; here we get y++=35++=36 but still y=35 only not 36 because it is y++, but this 36 value is kept in memory until the compiler moves to next line but still y=35 only...But due to keeping 36 in memory that value is taken and added with x++,since we know x++=20++=21,this value also kept in memory and still x=20 onlynow x=36+21 gives x=57 but still y=35 onlybuty=++y=++35 gives y=36 now only and then ++x=++57 gives x=58so now y=36+58=94now the result isx=57y=94I think so this explanation will be useful to u friends ----Regards S.Sathiyamoorthy.

Share on other sites

first want to know which compiler r u working....des 57 94 is correct when u r using turbo c compiler..but try in other compilers u will get different answers .. y because these types r called "undefined behaviors". we dont know which op will come..but according to turboc compiler above expl is correct...but considering all d eliments i will ans as "Undefined behavior".

Share on other sites

The answer is 57 94I explain you clearly don't be worried...First know that x++ will increment the value by one but the value didn't stored in x...and at the same time ++x increments the value of x and stores the new x's value in x...so inint x=20,y=35;x=y++ + x++; here we get y++=35++=36 but still y=35 only not 36 because it is y++, but this 36 value is kept in memory until the compiler moves to next line but still y=35 only...But due to keeping 36 in memory that value is taken and added with x++,since we know x++=20++=21,this value also kept in memory and still x=20 onlynow x=36+21 gives x=57 but still y=35 onlybuty=++y=++35 gives y=36 now only and then ++x=++57 gives x=58so now y=36+58=94now the result isx=57y=94I think so this explanation will be useful to u friends ----Regards S.Sathiyamoorthy..................i understood due to ur explaination only bro

Share on other sites

What will be printed as the result of the operation below:main() { int x=20,y=35; x=y++ + x++; //step 1 y= ++y + ++x; //step 2 printf(“%d%dn”,x,y);}

Ans: x=57 y=94Explanation: x = y++ + x++; Here y++ post increment means y++=35 after the y++ increment y becomes 36; x++ "" "" "" ---------------- x++=20 after the x++ increment x becomes 55 so result of step 1: x=55 After step 1 : x=56, y=36 step 2: y= ++y + ++x; Here x and y both are pre-increments so ++y = 37, ++x=557 y = 57 + 37 = 94

Share on other sites

I explain you clearly don't be worried...First know that x++ will increment the value by one but the value didn't stored in x...and at the same time ++x increments the value of x and stores the new x's value in x...

so in

int x=20,y=35;

x=y++ + x++; here we get y++=35++=36 but still y=35 only not 36 because it is y++, but this 36 value is kept in memory until the compiler moves to next line but still y=35 only...

But due to keeping 36 in memory that value is taken and added with x++,since we know x++=20++=21,this value also kept in memory and still x=20 only

now x=36+21 gives x=57 but still y=35 only

No sir you are mistaken. X is 56 here . Refer to the explanation below. If you dont believe me do a printf to get values at this point

but

y=++y=++35 gives y=36 now only and then ++x=++57 gives x=58

so now

y=36+58=94

now the result is

x=57

y=94

I think so this explanation will be useful to u friends ----Regards S.Sathiyamoorthy.

Ans: x=57

y=94

Explanation: x = y++ + x++;

Here y++ post increment means y++=35 after the y++ increment y becomes 36;

x++ "" "" "" ---------------- x++=20 after the x++ increment x becomes 55

so result of step 1: x=55

After step 1 : x=56, y=36

step 2: y= ++y + ++x;

Here x and y both are pre-increments so ++y = 37, ++x=557

y = 57 + 37 = 94

Yes sir you are quite correct

The only thing we need to remember here is that

x++ is a function which increments x but returns old value of x, where as ++x is a function that increments x and returns the new value of x.

Share on other sites

I agree with RAJEEV CHOWDARY answer.

Share on other sites

you got: X=57 and y=94

Share on other sites

I think the answer is x=57y=94in step1 post increment operator is present so the value of y increments to 36 and in same way the value of x incremented to 21.so after step 1 x=56, y=36Now in the step 2 x and y are pre incremented So after step 2 y =36+58=94so the over all result is x=57y=94