While reading through files included in /spl
library (Stanford Portable Code), downloaded with pset4.zip
distro, I came across this definitions in cslib.h
:
/*
* Type: bool
* ----------
* This type defines the space of Boolean data using the constants
* <code>false</code> and <code>true</code>.
*/
#ifdef bool
# undef bool
#endif
#ifdef false
# undef false
#endif
#ifdef true
# undef true
#endif
typedef enum {false, true} bool;
If I get it correctly, the goal is to redefine the boolean primitives as an enumeration.
In order to do that, first it checks whether they're already defined, and, if so, it "undefines" them. Only to re-state'em again, but as a typedef enum
.
So, my question is: why bothering doing this? If I get it correctly, they'll work just as "regular" bool
s do in 'C': they can have either one of two values true
or false
, and they can be represented alternatively as:
false
or0
(first element in the enumeration)true
or1
(second element in the enumeration)
Of course, there has to be a reason, I just can't spot it.
Is it so as not to include stdbool.h
? (why?)
What would be the advantage of redefining them as an enumeration rather than integer constants?
_Bool
wasn't introduced to C until C99.cslib.h
: it includesstdio.h
,stdlib.h
andstddef.h
. I think @Kareem has a point here.