Boolean Types

6.6 Types

Moderators: misra-c, david ward

Locked
djtachyon
Posts: 1
Joined: Thu Jan 18, 2007 6:59 pm
Location: New Jersey

Boolean Types

Post by djtachyon » Mon Feb 05, 2007 7:08 pm

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.

[email protected]
Posts: 2
Joined: Fri Oct 01, 2004 2:03 am
Location: Livonia, MI

Post by [email protected] » 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.
Walter W. Schilling, Jr.
2004-2005 Ohio Space Grant Consortium Fellowship Recipient
Uinversity of Toledo PhD Student
MSES 1998 University of Toledo
BSEE 1997 Ohio Northern University

Locked

Return to “6.6 Types”