15-02-2005, 12:33 PM
derek_farn Wrote:Rules then need
to be defined for the result of arithmetic operations.
For instance, is char_valu_1 + char_valu_2 a character
or numeric value? What about char_valu_1 + 1? Can I apply
the ++ operator to an object having char type?
Common sense would expect that the arithmetic operations are meaningful, this would be no problem for increment and decrement:
> char_value++ is the next character in the character table
> char_value-- is the previous character in the character table
The meaning of binary arithmetic operations are, as Derek already suggested, more interesting. If we treat a character like a pointer into a character table, the meaning would be well-defined:
> char_value + num_value is the character having the distance of num_value in the character table
> char_value_1 - char_value_2 is the distance between the two characters (a numerical value, but is it rather signed or unsigned?)
> char_value_1 + char_value_2 is not valid.
> other arithmetic operators are not allowed
Without having had a look into ISO-C I doubt that the defined behaviour matches with this meaning, and even if it does the source code will be hard to understand, thus being a potential source of bugs.
There are already too many pitfalls with automatic type propagation, so don't add more!
Conclusions:
(1) I would allow increment and decrement on characters (as long as you stay withing the character set and the character set has no gaps).
(2) I would not allow mixing character and numeric values or using binary arithmetic operators on character values UNLESS explicit type conversion functions with a well-defined behavior are used.