Jump to content
ChetanaSforum

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

The answer is 57 94

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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×