Connecting Tech Pros Worldwide Help | Site Map

Able to write integer to char without cast.

  #1  
Old July 7th, 2009, 08:16 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9
Is that normal behaviour? That is, x is of type char and when trying to then assign a number to x, no type errors are raised? However, if it was the other way around, char into int, it wouldn't compile.

Does this have to do with the memory sizes of types?

Excuse my ignorance, I'm a little old fool who's only ever really worked with dynamic 'languages'. :D
  #2  
Old July 7th, 2009, 08:25 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Able to write integer to char without cast.


Quote:
Originally Posted by Markus View Post
Is that normal behaviour? That is, x is of type char and when trying to then assign a number to x, no type errors are raised? However, if it was the other way around, char into int, it wouldn't compile.

Does this have to do with the memory sizes of types?

Excuse my ignorance, I'm a little old fool who's only ever really worked with dynamic 'languages'. :D
From the Standard:

Quote:
Originally Posted by C Standard
When an integer is demoted to an unsigned integer with smaller
size, the result is the nonnegative remainder on division by the
number one greater than the largest unsigned number that can be
represented in the type with smaller size. When an integer is demoted
to a signed integer with smaller size, or an unsigned integer is
converted to its corresponding signed integer, if the value cannot be
represented the result is implementation-defined.
No warning is issued so you can do what you did in an implementation defined way.

kind regards,

Jos

ps. Old?
  #3  
Old July 7th, 2009, 09:09 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9

re: Able to write integer to char without cast.


Quote:
Originally Posted by JosAH View Post
From the Standard:



No warning is issued so you can do what you did in an implementation defined way.

kind regards,

Jos

ps. Old?
Young wouldn't have worked...

But I am little.

Oh, and thanks.

Mark.
  #4  
Old July 8th, 2009, 02:53 AM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 804
Provided Answers: 3

re: Able to write integer to char without cast.


Quote:
Originally Posted by Markus View Post
However, if it was the other way around, char into int, it wouldn't compile.
I would have expected that to work just fine. What compiler message did you get?
  #5  
Old July 11th, 2009, 05:47 PM
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,338

re: Able to write integer to char without cast.


Markus is talking about automatic integer promotions:

If the value can be put in an int, then it is converted to an int
otherwise, it is converted to an unsigned int.
Now, if either operand is unsigned long, then the other is converted to unsigned long.
Now, if either operand is long, then the other is converted to long.
Now, if either operand is an unsigned int, then the other is converted to unsigned int.
Otherwise, both are ints.

Then the operator is performed and the type of the result is the type of the operands.


So, that char is converted to int. The calculation is done as an int and the result put in a char. It is your responsibility to insure the char can hold the result.

Likewise, there are separate automatic promotions for floating point.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM