Connecting Tech Pros Worldwide Forums | Help | Site Map

Able to write integer to char without cast.

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#1: Jul 7 '09
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

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jul 7 '09

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?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#3: Jul 7 '09

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.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 904
#4: Jul 8 '09

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?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,553
#5: Jul 11 '09

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