1

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" bools do in 'C': they can have either one of two values true or false, and they can be represented alternatively as:

  • false or 0 (first element in the enumeration)
  • true or 1 (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?

  • 2
    I'm not sure, but maybe for compatibility with C89 and later versions since the type _Bool wasn't introduced to C until C99. – Kareem Dec 9 '14 at 20:32
  • Would this allow the spl to be used independently and theoretically not require any additional libraries while still providing what would be considered a "standard" set of functionality? If so, that sounds like a good reason. – lethaljd Dec 10 '14 at 3:12
  • Well.. yes @lethaljd but, there're other standard libraries included. E.g. check cslib.h: it includes stdio.h, stdlib.h and stddef.h. I think @Kareem has a point here. – abelinux Dec 10 '14 at 3:21

You must log in to answer this question.

Browse other questions tagged .