MISRA Discussion Forums
Boolean Types - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.6 Types (https://forum.misra.org.uk/forumdisplay.php?fid=33)
+---- Thread: Boolean Types (/showthread.php?tid=338)



Boolean Types - djtachyon - 05-02-2007

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.


- walter.schilling@computer - 07-02-2007

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.