Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Booleans and the unterlying type
#1
There are no boolean types in C and I assume that rule 10.x also apply to boolean expressions. The question now is how to make
boolean expressions compliant to these rules.
If there are variables boolU8 and boolS8 which are of type unsigned char and signed char are represent boolean values, and assignment
will require an explicit cast:
boolU8 = (uint8)boolS8;
No problem here.

Boolean operators &&, || and ! yield a result of type "int" (MISRA-C:2004 page 37 3rd paragraph), which confuses me a little bit,
because I expected the guideline to specify an "underlying type" instead of a real type.
So to have a simple assignment like
boolU8 = c && d;
compliant, I have to downcast to the result to uint8, which requires a temporary variable (according to rule 10.3):
int _t = c && d;
boolU8 = (uint8)_t;

The is no more safe than the simple assignment, a lot less readable and involves the plain type "int", which violates rule 6.3 (typedefs
should be used in place of the basic types).

I did not find anything on this subject in the technical corrigendum, nor does the entry in the glossary on "boolean expressions" help
with type casting.

Any thoughts ?
<r>Gunter Blache<br/>
<br/>
ETAS GmbH<br/>
PSW<br/>
Global Operations<br/>
Borsigstraße 14<br/>
70469 Stuttgart<br/>
Phone +49 711 89661-159<br/>
Fax +49 711 89661-330<br/>
<br/>
<URL url="http://www.etasgroup.com">http://www.etasgroup.com</URL></r>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)