Thread: Is bool a good thing?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Is bool a good thing?

    It feels to me as though bool is a fundamental data type for computer programming. Storing true or false has obvious practical applications. Yet, some people seem to deprecate bool. It isn't in standard C89. And in any case, bool can be pretty easily simulated like this:

    Code:
    typedef bool unsigned int;
    #define true 1
    #define false 0
    I'd like to use something like this:

    Code:
    typedef enum blah { false = 0, true = 1 } bool;
    1. Do you consider bool to be a useful and necessary data type?
    2. How does one best simulate/replace bool for a system that doesn't support C99/C++?

    Richard
    Last edited by Richardcavell; 04-23-2011 at 08:15 AM. Reason: oops

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    1. Nope
    2. enum or #define

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Richardcavell View Post
    1. Do you consider bool to be a useful and necessary data type?
    Useful, yes, necessary, no. When I'm working in a language that has them, I'll use the type. In C, I'll just use an int set to 0 or some other number. Some people use hungarian notation like "bVariableName" to indicate bools in C; your method has a little more polish.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Mostly it's ok. But it's hardly necessary in C...

    Code:
    int Test(int x)
     { if (x > 10)
          return 1;
       return 0; }
    
    
    // call
    if (!test(myfancytoolongvariablename))
      printf("Dooohh\n");
    It's all nice and neat to define stuff like that but in reality you're just putting lipstick on a pig... nothing new is introduced because it's all still 0 and 1.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, even with respect to C99, an expression such as x > 10 results in an int (rather than _Bool, though it is possible that _Bool is int) that is either 0 or 1, so CommonTater's definition of Test could have been simplified to:
    Code:
    int Test(int x)
    {
        return x > 10;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Incidentally, even with respect to C99, an expression such as x > 10 results in an int (rather than _Bool, though it is possible that _Bool is int) that is either 0 or 1, so CommonTater's definition of Test could have been simplified to:
    Code:
    int Test(int x)
    {
        return x > 10;
    }
    Yep... but I was pretty sure he wouldn't understand it so I went with the more obvious version.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Not necessary in C perhaps, but it can certainly aid readability.
    E.g. Say you found some code calling a homemade 'find' function. No code comments are present. Is it returning the index of the item found or is it returning one for found and zero for not found?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Not necessary in C perhaps, but it can certainly aid readability.
    E.g. Say you found some code calling a homemade 'find' function. No code comments are present. Is it returning the index of the item found or is it returning one for found and zero for not found?
    That should be easily dscernable from the return statments... return A[index] is pretty much a give away.

    Still your point is taken, code with TRUE and FALSE as text is often more explicit than 1 and 0... but that alone should not render it inscrutable.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you have stdbool.h available it's IMO preferable to defining true/false yourself. At least if used explicitly to compare if something is true. For example:

    Code:
    if( test == true )
        // do something
    Works if test is defined as bool, but if test is an int, is only true if test happens to be 1.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    That should be easily dscernable from the return statments... return A[index] is pretty much a give away.
    Oh, I dunno. I reviewed some code a while back which did exactly that. The thing is, the caller had been required to ensure A[index] was either 0 or 1, and nothing else.

    A willful programmer can make even simple things inscrutable.

    In answer to the original question, bool is useful but not essential. In languages that don't have it, I don't attempt to emulate it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bool = c++?
    By chico1st in forum C Programming
    Replies: 37
    Last Post: 05-22-2008, 03:43 AM
  2. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 08:39 AM
  3. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:24 AM
  4. using classes is good thing?
    By Leeman_s in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2002, 03:16 AM
  5. Interesting: bool != BOOL
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 11:09 AM