Page 1 of 1

Boolean Types

Posted: Mon Feb 05, 2007 7:08 pm
by djtachyon
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.

Posted: Wed Feb 07, 2007 4:24 pm
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.