Thread: bool statements in C?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    bool statements in C?

    Are boolean statements supported in C?
    if not how would you work around this?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Are boolean statements supported in C?
    Absolutely. What are you trying to do?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    when I declare bool whatever = true; I get compiler error "bool undeclared identifier"

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Are you using C99? If so, this should work just peachy:
    Code:
    #include <stdbool.h>
    
    int main ( void )
    {
      bool whatever = true;
    }
    But in the more likely case that you're using C89, you'll need to use an int.
    My best code is written with the delete key.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    Prelude once told me to use this as an alternative.
    Code:
    typedef enum {true, false} bool;
    However, using an int is just as easy and just as useful.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Or a char . . .

    TRUE and FALSE are fairly common macros.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Ok, I got it working. On my box, I had use TRUE && FALSE in caps with out declaring bool ,ie:
    Code:
    int main ( void )
    {
      whatever = TRUE;
    }
    Thanks all !

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Our mistake, we assumed you meant a standard implementation...


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by sand_man
    Prelude once told me to use this as an alternative.
    Code:
    typedef enum {true, false} bool;
    However, using an int is just as easy and just as useful.
    It might be a little more intuitive to switch the order of true and false in your enum..

    Code:
    typedef enum {FALSE, TRUE} boolean;

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Prelude once told me to use this as an alternative.
    Actually, it was probably more like this due to the common use of true, false, and bool:
    Code:
    typedef enum { BFALSE, BTRUE } bool_t;
    One thing I certainly like about C++ is the introduction of namespaces to help with naming collisions.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 08:43 PM