Home › Forums › C Programming › doubt on comparision operator
- This topic has 2 replies, 3 voices, and was last updated 17 years, 3 months ago by Shailesh Kumar Gautam.
- AuthorPosts
- June 5, 2007 at 8:20 pm #1977wadhwasharpParticipant
Hithe following code is giving 1 as answer but answer should be 0 …becoz 0.7 is not grater than 0.7
123456<br />int main(){<br />float a=0.7;<br />printf("%d",(0.7>a));<br />}<br />please tell me the reason
- June 12, 2007 at 5:35 pm #3231willParticipant
@drop2kumar wrote:
Hi
the followiing code is giving 1 as answer but answer should be 0 …becoz 0.7 is not grater than 0.71234int main(){<br />float a=0.7;<br />printf("%d",(0.7>a));<br />}
Actually you are comparing the float type variable with a value which can be any thing. It can be an integer, float or any thing else. So in that case compiler does not know about the type.
Now if you explicitly mention the type of both the variables and then compare them the result would be according to your expectations. Hopefully this would clear your doubt.12345678<br />int main(){<br />float a=0.7;<br />float b=0.7;<br />printf("%d",(b>a));<br />printf("%d",(0.7>a));<br /><br />} - September 10, 2007 at 6:28 am #3232Shailesh Kumar GautamParticipant
Actually when we compare, compiler checks the type.. one u declared as float and other is number.. the default one the compiler takes in decimal format as double.. So, in representation double as 8 bytes and accurate value is entered.. while float contains 4 bytes and is comparitively less accurate than double.. So, double having atleast some minute value greater than float value.. thats why its value is true and hence value is one..
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.