11-11-2009, 09:40 AM
Rule 10.6 is not relevant to the example which you quote ("result = -1 - 10;").
It is unfortunate that Rule 10.6 is located in the section devoted to "Arithmetic type conversions". This is misleading because the rule has nothing to do with type conversions. It has to do with the requirement to apply a "U" suffix to a constant IF the constant is of unsigned type.
The type of an integer constant depends on several things:
a) The magnitude of the constant
b) The implemented size of the various integer types
c) The number base in which the constant is expressed
d) The presence or absence of suffixes
The type of a constant does NOT depend on the context in which the constant is used.
In the example quoted, there are 2 integer constants "1" and "10", both of which are of type "signed int". Note that "-1" is NOT a constant; it is a "constant expression" - i.e the constant "1" preceded by the unary minus operator.
See ISO:C90 6.1.3.2
"The type of an integer constant is the first of the corresponding list in which its value can be represented.
This means that in an implementation with a 16 bit int and 32 bit long:
Rule 10.6 is only relevant to constants of large value and requires that a "U" suffix should be appended to both these constants - in order to make it obvious that they are unsigned constants.
The question of whether a signed or unsigned constant should be used in a particular context is addressed in Rule 10.1. Rule 10.1 demands (among other things) that a constant assigned to a signed object should be of signed type and a constant assigned to an unsigned object should be of unsigned type. It can be argued that the latter requirement is a little pedantic.
However, where the type of a constant is more significant is in expressions where "type balancing" occurs - what the ISO standard describes as " the usual arithmetic conversions". The intention of Rule 10.1 is to ensure that type balancing never occurs between two operands of different signedness. This is to ensure that the signedness of an expression is never ambiguous. Consider for example, the expression "u16a - 100", an operation involving an unsigned variable and a
constant of type signed int; if an int is implemented in 16 bits the result will be of type unsigned int. If an int is implemented in 32 bits the result will be of type "signed int" (and could be negative).
To reiterate: Rule 10.6 applies only to integer constants that, according to the C standard, are of unsigned type.
It is unfortunate that Rule 10.6 is located in the section devoted to "Arithmetic type conversions". This is misleading because the rule has nothing to do with type conversions. It has to do with the requirement to apply a "U" suffix to a constant IF the constant is of unsigned type.
The type of an integer constant depends on several things:
a) The magnitude of the constant
b) The implemented size of the various integer types
c) The number base in which the constant is expressed
d) The presence or absence of suffixes
The type of a constant does NOT depend on the context in which the constant is used.
In the example quoted, there are 2 integer constants "1" and "10", both of which are of type "signed int". Note that "-1" is NOT a constant; it is a "constant expression" - i.e the constant "1" preceded by the unary minus operator.
See ISO:C90 6.1.3.2
"The type of an integer constant is the first of the corresponding list in which its value can be represented.
- Unsuffixed decimal: int, long int, unsigned long int;
- Unsuffixed octal or hexadecimal: int, unsigned int, long int, unsigned long int;
- Suffixed by the letter u or U: unsigned int, unsigned long int;
- Suffixed by the letter l or L: long int, unsigned long int;
- Suffixed by both the letters u or U and l or L: unsigned long int."
This means that in an implementation with a 16 bit int and 32 bit long:
- "0x8000" has type unsigned int
- "2147483648" has type unsigned long
Rule 10.6 is only relevant to constants of large value and requires that a "U" suffix should be appended to both these constants - in order to make it obvious that they are unsigned constants.
The question of whether a signed or unsigned constant should be used in a particular context is addressed in Rule 10.1. Rule 10.1 demands (among other things) that a constant assigned to a signed object should be of signed type and a constant assigned to an unsigned object should be of unsigned type. It can be argued that the latter requirement is a little pedantic.
However, where the type of a constant is more significant is in expressions where "type balancing" occurs - what the ISO standard describes as " the usual arithmetic conversions". The intention of Rule 10.1 is to ensure that type balancing never occurs between two operands of different signedness. This is to ensure that the signedness of an expression is never ambiguous. Consider for example, the expression "u16a - 100", an operation involving an unsigned variable and a
constant of type signed int; if an int is implemented in 16 bits the result will be of type unsigned int. If an int is implemented in 32 bits the result will be of type "signed int" (and could be negative).
To reiterate: Rule 10.6 applies only to integer constants that, according to the C standard, are of unsigned type.
Posted by and on behalf of the MISRA C Working Group