Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting question

 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I have this silly doubt about casting and any clarification is appreciated.

I understand the byte, short , integer, long are 8,16,32,64 bits respectively.

so when I do this:



..the compiler does not complain even though '9' an integer literal of 32 bits is assigned to a 16 bit, is that right?

On the other hand


I dont understand why..

Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned in the other topic, the first rule that you are referring to, is related to assignment of compile time constants. During assignments, if the compiler is able to figure out, at compile time obviously, that the value will fit, then no cast is needed.

And there is no such related rule for method calls.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic