Thursday, July 30, 2009

Is n1k = (200<fChange<100) / 100; a legal statement in C programming?

is


n1k = (200%26lt;fChange%26lt;100) / 100;





a legal staement in C programming?if sho what does it mean?

Is n1k = (200%26lt;fChange%26lt;100) / 100; a legal statement in C programming?
yes it is right.


but what will be the output?


it will be 0 if n1k is int;


it will be 0.0000000 ot 0.0010000 if n1k is float.
Reply:Sure it's valid...though n1k will ALWAYS equate to 0





Reason: n1k is assigned the value of ( bool ) / 100





-The boolean value returend will always return false ( or 0 ) -fChange can never be bigger than 200 and smaller than 100 at the same time


-0 divided by or multiplied by anything will always return 0
Reply:It is legal C syntax but it is not very logical. I have no idea what you are trying to do. Even if your intention was this: (200 %26lt; fChange %26amp;%26amp; fChange %26lt; 100) it still doesn't make sense because fChange will never be greater than 200 and less than 100.





It is legal, and will compile, because in C a boolean result is 0 or 1. In the following code, "a" will be either 0 or 1 and so "b" will always be 1. However, "1/100" evaluates to 0 because it is an integer result.








#include %26lt;stdio.h%26gt;


void main();





void main()


{


int a, b;


float c, n1k, fChange = 205;





a = 200 %26lt; fChange;


b = a %26lt; 100;


c = b / 100;


n1k = (200 %26lt; fChange %26lt; 100) / 100;





printf("\na = %d\nb = %d\nc = %f\nn1K = %f\n", a, b, c, n1k);


}








With fChange==5 the output is:





a = 0


b = 1


c = 0.000000


n1k = 0.000000








With fChange==205 the output is:





a = 1


b = 1


c = 0.000000


n1k = 0.000000


No comments:

Post a Comment