How 3 and 3.0 are equal plz explain

M

manish sahu

what would be the output

if(3==3.0)
{
printf("HI");
}
else
{
printf("Bye");
}


op is HI
how 3==3.0 plz Explain
 
T

Tim Prince

manish said:
what would be the output

if(3==3.0)
{
printf("HI");
}
else
{
printf("Bye");
}


op is HI
how 3==3.0 plz Explain
If your C textbook doesn't explain how 3==3.0 is equivalent to
(double)3 == 3.0
you need a better textbook.
I would prefer that a C textbook would explain questions such as when
such casts are exact, but that preference does go beyond reality.
 
T

Thad Smith

manish said:
what would be the output

if(3==3.0)
{
printf("HI");
}
else
{
printf("Bye");
}


op is HI
how 3==3.0 plz Explain

C compares values. The two constants have the same value when compared.
 
K

Keith Thompson

Thad Smith said:
C compares values. The two constants have the same value when compared.

Well, they have different types. Specifically, 3 is of type int, and
3.0 is of type double. The standard specifies rules for converting
operands of "==" to a common type. In this case, the value 3 is
converted from int to double, and the result if this conversion is
compared to 3.0. Converting the int value 3 to double will almost
certainly yield the value 3.0, though I don't think the standard
quite guarantees it.
 
Joined
Aug 16, 2017
Messages
1
Reaction score
0
what would be the output

if(3==3.0)
{
printf("HI");
}
else
{
printf("Bye");
}


op is HI
how 3==3.0 plz Explain


Well it must be int something say x=3 and something float sat y=3.0 In C when there is an operation between an int and a float ,the int is converted into float.In this example 3 is converted to 3.0 and then the comparison is made.Hence 3 == 3.0.Hope this works!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top