Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Boolean Types
#1
Just wondering what is the best way to define a boolean type for MISRA and most importantly what to define TRUE and FALSE as.

Some thoughts:

typedef unsigned char boolean_t
#define TRUE (0==0)
#define FALSE (!0)

Thanks.
#2
MISRA really does not specify a specific mechanism for declaring boolean values. I have personally preferred the following:

typedef enum {FALSE, TRUE} boolean;

to be declared in a common header file. This creates a type (boolean) that can be used just as any other type. The disadvantage of this mechanism if one is using a small part (i.e. 8 or 16 bit part) is that the space for an integer will be used to store \"TRUE\" or \"FALSE\", which if you have a significant number of variables declared of this type can lead to a significant memory usage.

If memory is a problem, then I have seen the following set of declarations used:
typedef uint8_t boolean;
#define FALSE ((boolean)0)
#define TRUE ((boolean)1)

You also need to look at your compiler and make certain that you are compatable with its definition, if one exists, especially if you are using any library code.

So long as you are consistant and only use ONE of these mechanisms, any one will work. The real danger comes in if you have multiple declations being used, in which case serious problems can result.

Hope this helps.
<t>Walter W. Schilling, Jr.<br/>
2004-2005 Ohio Space Grant Consortium Fellowship Recipient<br/>
Uinversity of Toledo PhD Student<br/>
MSES 1998 University of Toledo<br/>
BSEE 1997 Ohio Northern University</t>


Forum Jump:


Users browsing this thread: 1 Guest(s)