473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

void main()
{
int x=3;
float y=3.0;
if(x==y)
printf("equal");
else
printf("not");
}
Oct 13 '10 #1
2 6618
weaknessforcats
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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

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

Similar topics

0
by: Thomas Mlynarczyk | last post by:
Hello! I want a table-like layout (2 x 2 cells), but as it's not tabular data I want to do it with divs and CSS. Here's what I have come up with so far (using an HTML 4.01 Strict doctype): ...
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
9
by: PagCal | last post by:
Is this the default and expected behavior for vb.net? 'C' and 'C++' used to skip b() if a() was not true, because there was no way the IF expression would ever be true. For example, if you did...
32
by: barkarlo | last post by:
In my table date/time field (starttime,endtime) include both date and time. How can I calculate worktime in query for each employee who works sundays? for an example: sometimes starttime and...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
2
by: Eric Lindsay | last post by:
I hope someone can help, before I start thinking tables have a lot of merit after all. I am trying to achieve a two column layout, with source code div order main content, right side navigation,...
2
by: metalheadstorm | last post by:
Ok this is my first post, gtz me ^^ well i have a DB im making for a project and im using a Vb interface and a access db linking them togethering using ADO. on one SStab / form i have a...
3
by: goelvivek | last post by:
#include<stdio.h> #include<conio.h> void main() {double i=1.1; float j=1.1; clrscr(); if(i==j) printf("both are equal"); else printf("both are not equal");
2
by: vitaminz | last post by:
Hi, I'm currently coding a Java program for a University coursework, and I'm a little bit stuck. How do I find out if a string is NOT equal to a certain value? I know to test if it is equal, you...
1
bonski
by: bonski | last post by:
Good day to all, Recently I have encountered a problem regarding to float numbers. I have this query that gets the values from database and insert it to an array. These values are decimal (float)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.