How to define the boolean in C

This is a discussion on How to define the boolean in C within the C Programming forums, part of the General Programming Boards category; Hi All, I want to define a variable by type boolean in C and I don't know how. Is there ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    How to define the boolean in C

    Hi All,

    I want to define a variable by type boolean in C and I don't know how. Is there anyone know, please help me? Thanks a lot!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You could us an enummeration for that.

    Code:
    typedef
    {
        FALSE,
        TRUE
    } boolean;
    Or, if your compiler supports the latest C-standard (C99), you can include stdbool.h and use the bool-type.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thanks Shiro.
    I wonder when you coded like that, TRUE = 1 and FALSE = 0, right?
    Code:
    typedef
    {
        FALSE,
        TRUE
    } boolean;
    
    struct dataInfo {
       boolean DataReady;
    }
    
     So in later in program, I put the check statement 
    
    if (DataReady)
      { .... doing someting;
         DataReady = FALSE;
       }

    Is this OK?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Yes, FALSE == 0.

    Hmmm, I made a mistake. Sorry, it should be:

    Code:
    typedef enum
    {
      FALSE,
      TRUE
    } boolean;

    >if (DataReady)

    That is OK, if this DataReady is a standalone variable and not a member of dataInfo.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    => That is OK, if this DataReady is a standalone variable and not a member of dataInfo

    What do you mean by saying that? Sorry for slow understanding.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I referred to this:

    > struct dataInfo {
    > boolean DataReady;
    > }

    Take a look at this:

    Code:
    /* A variable of type struct dataInfo, containing DataReady. */
    struct dataInfo dataInfo_variable;
    
    /* A "stand-alone" DataReady variable. */
    boolean DataReady;
    
    /* Use DataReady as stored in the structure-variable. */
    if (dataInfo_variable.DataReady == FALSE)
    {
    }
    
    /* Use the "stand-alone" DataReady variable. */
    if (DataReady == FALSE)
    {
    }
    Probably "stand-alone" is not the correct word, but I don't know the correct English word for it.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    I got it. Thanks a bunch.

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Shiro

    Probably "stand-alone" is not the correct word, but I don't know the correct English word for it.
    From the standard :
    The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types. The integer and real floating types are collectively called real types.
    A real type might be the proper expression here.
    The one who says it cannot be done should never interrupt the one who is doing it.

  9. #9
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124
    just an idea:
    typedef enum boolean { False, True} bool_t;
    Man's mind once streched by a new idea, never regains its original dimensions
    - Oliver Wendell Holmes

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
    - WaltP

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Why not

    Code:
    #define TRUE 1
    #define FALSE 0

    Isn't this the simplest?

    Also, C99 does support boolean type. Use it if you can.
    Mr. C: Author and Instructor

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Isn't this the simplest?
    It depends on your point of view. I personally prefer a specialized type defined as boolean (for C89) to aid in readability:
    Code:
    typedef enum { BFALSE, BTRUE } bool_t;
    Now instead of int you declare a bool_t, I consider this to be more readable. Your mileage may vary.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I'll go with the same way as Mr.C:
    Code:
    #define TRUE 1
    #define FALSE 0
    This isn't the most elegant, but it works
    In your program you can do this:
    Code:
    bLegalVal = FALSE;
    But this isn't a new type, to use the bool type, define him with typedef as people told you here.

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >This isn't the most elegant, but it works

    Using a defined type also works, in fact it works almost the same way, but it is more readable. The constant values in the enum can be used just like the #define'd values. Look at this:

    Code:
    int a;
    boolean b;
    From variable b it is immediately clear that it is a boolean-variable, it is not clear from variable a. In my opinion it is good to make a program elegant and readable.

  14. #14
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ohh, I see now shiro, it really looks strange something like that:
    Code:
    int bCorr = FALSE
    It's really more readable:
    Code:
    boolean bCorr = FALSE
    I didn't think about it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 06:33 AM
  2. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 02:53 PM
  3. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 04:07 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 11:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21