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

(#61) Can you automatically/implicitly convert a char to a short?

 
Pawel Nowacki
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer for this :
"No. They're the same bit-depth, but since chars are unsigned they might have a higher positive value than a short can accept"

I quite don't understand this. If by saying "automatically/implicitly convert a char to a short" we mean this:



this compile and run ok, and i did it automatically/implicitly.
Now if we try to assign character which value is greater than 32767 then i understand that this will cause compilation error but does it mean i can't
"automatically/implicitly" try to do it (just like in casting)?
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a interresting question. What is the answer?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Nowacki wrote:The answer for this :
"No. They're the same bit-depth, but since chars are unsigned they might have a higher positive value than a short can accept"

I quite don't understand this. If by saying "automatically/implicitly convert a char to a short" we mean this:



this compile and run ok, and i did it automatically/implicitly.
Now if we try to assign character which value is greater than 32767 then i understand that this will cause compilation error but does it mean i can't
"automatically/implicitly" try to do it (just like in casting)?


Your example isn't taking a char and implicitly converting it to a short. You are just assigning the code for 'c' to a short.



This code declares little as a char and gives it the value 'c'. The next line declares t as a short, and tries to pass the value of 'little' into it. When the compiler tries to implicitly convert little to a short it fails, giving the following error:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic