"bool" in C

This is a discussion on "bool" in C within the C Programming forums, part of the General Programming Boards category; Hi, I have (unfortunately) found out that C doesn't support the "bool" keyword. So what is the most common (or ...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question "bool" in C

    Hi,
    I have (unfortunately) found out that C doesn't support the "bool" keyword. So what is the most common (or best) way to use boolean statements in my code?
    I have tried this:
    Code:
    typedef int bool;
    and then I used "0" instead of "false" and "1" instead of "true".

    But this seems to me a little "waste" of memory. I just want to store only 2 values (true and false) but I use "4 bytes" (sizeof(int)) instead. So maybe it would be better to use this:
    Code:
    typedef char bool;
    and occupy only "1 byte" (sizeof(char)) of memory. But what should then I use instead of "false" and "true"? For example 'n' and 'y', or what?

    Thanks.
    Petike

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    If you are compiling in C99, you could #include <stdbool.h> and use bool, true, and false. bool would be a macro for _Bool, which itself is a standard type guaranteed to hold 0 or 1, and true and false are macros for 1 and 0 respectively.

    If not, you could just use 0 and 1 directly. I would not be too concerned about wasting memory, unless it really is a problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Just use 0 & 1.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Quote Originally Posted by laserlight View Post
    If you are compiling in C99, ...
    Just a little question. What is "C99"? I have seen that also in some other posts here, but I have never seen it before? It is some type of compiler?
    By the way, I use "Microsoft Visual Studio 2008".
    Petike

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Petike
    What is "C99"?
    The 1999 edition of the C Standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It usually exists on most compilers, even C89 ones. So if it exists use it, otherwise define your own macros.

    The compiler may even optimize (I haven't checked), so don't worry about the memory.
    Last edited by zacs7; 11-22-2008 at 03:49 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Visual Studio does not have support for the 1999 C standard, though.
    Also, a char can hold 0 and 1, since it is basically an integral (characters are actually translated into integers).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Code:
    #if (__BORLANDC__ <= 0x460) || !defined(__cplusplus)	// Define o Tipo Booleano para o C
      typedef enum { false, true } bool;
    #endif

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    Code:
    #if (__BORLANDC__ <= 0x460) || !defined(__cplusplus)	// Define o Tipo Booleano para o C
      typedef enum { false, true } bool;
    #endif
    That's a fairly specific implementation, but yes, that sort of thing will work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You can also define true and false
    Code:
    #define true 1
    #define false 0
    typedef char bool;
    and use it normaly, like bool h = false (with the obvious risks of using #define)

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by sergioms View Post
    Code:
    #if (__BORLANDC__ <= 0x460) || !defined(__cplusplus)	// Define o Tipo Booleano para o C
      typedef enum { false, true } bool;
    #endif
    stdbool.h is guaranteed to work in macros. You've just broken that gaureentee.

    Quote Originally Posted by C_ntua View Post
    You can also define true and false
    Code:
    #define true 1
    #define false 0
    typedef char bool;
    and use it normaly, like bool h = false (with the obvious risks of using #define)
    Except they might already be defined, so undefine them first :-). Using the man page as a reference...
    Code:
    #ifdef __bool_true_false_are_defined
    #   undef true
    #   undef false
    #   undef bool
    
    #   define true 1
    #   define false 0
    #   define bool char
    #endif
    Last edited by zacs7; 11-23-2008 at 05:06 PM.

  12. #12
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Don't worry about wasting space (if you still are use unsigned short int which "wastes" as much space as a char). I agree w/ cpjust, use 0 = FALSE and non-zero = TRUE (where non-zero would probably be 1) and just check for > 0. Not that hard, also no point having a type that can only hold 0 or 1 when you use it proparly.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Btw, can you define " true ", rather than "true" (with two extra spaces) so you wont change a variable like istrue to is1 ?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Macros cannot contain spaces, but you can easily append _ before or after.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    But what if I wanted, "is_true_and_stuff" :-)

    Y'arrh the pre-processor be a harsh mistress.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "bool" in Visual C++
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 11:01 PM

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