• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit Casting

 
prakash sodhani
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am kind of confused with casting.

Let say if we have

float f=10;

When i executed this it works fine. My question is...if 10 is interpreted as double (64 bits)...whats the reason above assignment passed.

Same is the case with following:

long w=10;
float m=w;

When is the implicit casting done???

Thanks
Prakash
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 10 is not interpreted as a double. It's an int literal constant.

The conversion is allowed because int -> long -> float is allowed. This is a widening conversion.

The constant's value 10 is small enough to fit into a float without losing any information (precision). Even if precision is lost you will not get an error.
[ November 07, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checkout Corey's Article On this Topic
 
prakash sodhani
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry. This article seems to be what I was looking for.

-Prakash
 
PNS Subramanian
Ranch Hand
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,

int -> long to ok.
Is long -> float allowed ? Thought long was 8 bytes and float was 4 - could you please clarify.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You too can write code:


Output:
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Checkout Corey's Article On this Topic


Hehe - sometimes, Barry, I think you remember what I write better than I do. I had forgotten about that article.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic