Embedded Wednesdays: Bools
This week on Embedded Wednesdays we are looking at something relatively new, the bool data type in the C language.
First, what is a bool? Bool is short for boolean. It is a data type that has two possible values, 0 (false) and 1 (true). Introduced in the 1999 version of C (C99), it is used to store the result of a logical comparison.
“Wait, what?” you say, “bool isn’t new! I’ve been using booleans in my C code for years.” The concept of storing boolean values existed since the beginning of the C language, but only as an integer being used to represent a boolean value. Now boolean is a data type built in to the language.
To formalize the support for all of this, C99 introduced a base type called _Bool and a header file called stdbool.h. Stdbool provides a synonym of bool that is a bit more convenient than _Bool, as well as symbols for true and false, so you don’t have to use 1 and 0.
C has always had a rule that any integer is considered to be “false” if it has a value of zero, and any other value is considered “true”. People have used this method for years. The introduction of bool avoids a few unsavory side effects in the previous method.
Here’s an example of how bools work now:
#include < stdio.h>
#include < stdbool.h>
void main(void) {
bool isDone;
isDone = true;
if (isDone) {
printf("We're done\n");
} else {
printf("Not done yet\n");
}
}
Beware
When dealing with older C code, you will run into situations where the author used the rule of false being equal to zero and everything else being true in a dangerous way.
You may see something like the following where the author would use a typedef statement to define a synonym for an integer and use it as a boolean:
#include < stdio.h>
#include < stdint.h>
typedef uint8_t boolean;
void main(void) {
uint8_t i;
boolean isDone;
i = 42;
isDone = i;
printf("%d\n", isDone);
}
In this code, the value of isDone is printed out as 42. (What happens when i is assigned a value of 256?)
When using the C99 bool type, something different happens:
#include < stdio.h>
#include < stdint.h>
#include < stdbool.h>
void main(void) {
uint8_t i;
bool isDone;
i = 42;
isDone = i;
printf(“%d\n", isDone);
}
The value of isDone is 1. Even though we assigned it a value of 42. Since it is a bool type, it gets the value 1 on assignment.
An issue can crop up when the author assumes that the boolean value has a value other than 0 or 1.
Bad Habits
Here’s a bad habit, using a hard to remember rule:
#include < stdio.h>
#include < stdint.h>
void main(void) {
uint8_t i;
i = 42;
if (i) {
printf("true\n");
}
}
The construct if ( i ) is ambiguous to humans, now the reader thinks, “If I what? How does the rule go? 0 is true and 1 is false, or the other way around?” This way of doing things just confuses the person that has to maintain the code.
"Wait, wait, wait", you say, "isn't if ( i ) the same as if ( isDone) that you used in the first example?" No, it isn't. Don't be so boolean, so black and white, isDone gives an indication of the intent of the variable, i is normally a counter, so if ( i ) doesn't hint at your intent.
A better way to express this is to say what you mean: if ( i == 0). Better yet, use a bool variable and give it a name that indicates what it is used for:
int32_t i;
bool isDone;
isDone = false;
if ( i == ARRAY_SIZE) {
isDone = true;
}
Boolean types in C are relatively new and quite straight forward, but historical tricks can bite you.
Next week we will talk about arrays. We’ve seen them when talking about chars, but we’ll go through the intricacies. Until then, you might want to try some of the example code, there are free C compilers for your PC. Start playing.
Free C compilers you say?
- Linux: sudo apt-get install gcc
- MacOS - in the app store, download Xcode.
- Windows - I believe you should be able to use Microsoft Visual Studio Express. Your system is probably polluted with 2 or 3 copies already.
- Linux/MacOS/Windows - install Eclipse/CDT, I believe you will get a C compiler with the install.
- Embedded systems - read the ESE101 posts by Chris Svec. For ST Microelectronics processors, I use the tools from openSTM32.org that work on Linux, MacOS, and Windows.
This post is part of a series. Please see the other posts here.