By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

what is the reason of giving output is ravi except rahul

P: 2
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. float p=0.7;
  6. if(p==0.7)
  7. printf("rahul");
  8. else
  9. printf("ravi");
output: ravi

Aug 31 '10 #1
Share this Question
Share on Google+
3 Replies

P: 687
0.7 seems to be a popular number -
Aug 31 '10 #2

Expert Mod 5K+
P: 9,197
You cannot use the == operator with floating point.

Due to rounding, numbers that are close in value may test equal.

Ditto for !=.
Sep 2 '10 #3

P: 4
In this kind of comaprisions, 99% of the time the else part of the if condition gets executed.

In this example, when you try to compare 0.7 with 0.7, what actually happens internally is, one of the 0.7 is interpreted as 0.6999999999.

Floating point representation is machine dependent.

Even though there is not much of a difference between 0.7 and 0.6999999999, the if condition fails.

So, never use floating numbers for comparision.
Sep 3 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.