rbutterworth@orchid.waterloo.EDU (Ray Butterworth) (08/20/87)
The problem with trying to fake a Boolean type in C is that a Boolean variable may end up with a value that isn't restricted to 0 and 1. One way to help prevent such mistakes is to have Boolean as an enumerated type, with a macro that turns an expression into exactly one of the two values. e.g. typedef enum {FALSE=0, TRUE=1} Boolean; #define TRUTH(expression) ( (expression) ? TRUE : FALSE ) But of course this can generate fairly inefficient code in many common cases, such as when the expression is already a 0 or 1. #define TRUTH(expression) ((Boolean) ((expression)!=0)) or #define TRUTH(expression) ((Boolean) (!!(expression))) should generate better code. On BSD 4.3 though, the third example generates much better code than the second. e.g. try the following: extern int a, b, c; a = ((b==c)!=0); a = !!(b==c); The compiler does well with the double not, but fails to optimize the logically equivalent double comparison verison. Perhaps this is an obvious optimization that BSD might want to add to its compiler?