Monday, May 24, 2010

Solve this C puzzle?

int main()


{


float x=1.3;


if(x%26lt;1.3)


printf("Hello");


else printf("Hi");


return 0;


}


Output of this program is-%26gt;Hello


Why???????

Solve this C puzzle?
by default, C consider all numbers(i.e. both float and double) as double. For eg,here 1.3 is taken as double.





x is a float and C allocates 4 bytes for it.


and 1.3 is a double (at comparison) and C allocates 8 bytes for it.





so, x %26lt; 1.3 is succeeded and it prints "Hello".


It is because of Memory Allocation. For futher details, refer any book regarding how memory is allocated for float and double.
Reply:because of the float definition...use integer
Reply:add an f to the right hand sides of the numbers 1.3. that tells the compiler (if it supports) the number is actually float. or do type casting. sth like this:


float x=float(1.3)


if (x%26lt;float(1.3))


{.


.


.}


if this doesnt work, then im quitting programming.


No comments:

Post a Comment