09-06-2008, 12:19 PM

I have a question about underlying type of expressions involving unary minus operator. This is connected to 10.1 rule that disallows signed/unsigned conversions.

For example:

What is the underlying type of "-mc2_1209_u8a" expression? If it is unsigned then rule 10.1 is not violated; if it is signed then the rule is violated. (Of course this code is a violation of rule 12.9 anyway).

In my opinion the underlying type is still uint8 because C standard says in 6.5.3.3[3] (C99 cited):

This seems consistent with rule 12.9 which disallows using unary minus operator on unsigned operands.

Please correct me if I am wrong or if this was not the intention of MISRA rules.

For example:

Code:

`uint8_t mc2_1209_u8a;`

mc2_1209_u8a = -mc2_1209_u8a;

What is the underlying type of "-mc2_1209_u8a" expression? If it is unsigned then rule 10.1 is not violated; if it is signed then the rule is violated. (Of course this code is a violation of rule 12.9 anyway).

In my opinion the underlying type is still uint8 because C standard says in 6.5.3.3[3] (C99 cited):

Quote:The result of the unary - operator is the negative of its (promoted) operand.

The integer promotions are performed on the operand, and the result has the

promoted type.

This seems consistent with rule 12.9 which disallows using unary minus operator on unsigned operands.

Please correct me if I am wrong or if this was not the intention of MISRA rules.