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

int x=3;float y=3.0; if(x==y) is both equal if yes why???

P: n/a
void main()
{
int x=3;
float y=3.0;
if(x==y)
printf("equal");
else
printf("not");
}
Oct 13 '10 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You can't use the == operator on floating point variables.

Values that are close will test as equal due to rounding.

You need to compare the values as one less than the other within an allowed margin of error.
Oct 13 '10 #2

Expert 100+
P: 2,398
What are you trying to accomplish?

Is this perchance a homework assignment that seeks to test your ability to understand this code? I suggest your review the required behavior of the C compiler when an operator (in this case '==') has operands of different types (in this case int and float).
Oct 13 '10 #3

Post your reply

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