Thread: Boolena in C?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    Boolena in C?

    How to declare boolena in C?

    I tired to do

    bool a;

    And it didn't work for me gave

    'bool' undeclared (first use in this function) , while compiling.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    c does not have a Boolean data type
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Try this:

    Code:
    #define TRUE 1
    #define FALSE 0
    
    char a;
    a = TRUE;
    if(a)
        a = FALSE;
    You use the char type instead of int to save memory.
    Last edited by ITAmember; 05-31-2009 at 06:39 PM. Reason: char's a type not a value :P

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Unless you learned to program after 1999, in which case C does have a boolean data type (called _Bool).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    55
    how to use _Bool?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I usually type '_', then 'B', then 'o', then another 'o', and then 'l'.

    Code:
    _Bool a_boolean_variable;
    If you also #include <stdbool.h>, then you also get "true" and "false", as well as the privilege of saying "bool" instead of "_Bool".

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    55
    thanx a lot

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ITAmember View Post
    Try this:

    Code:
    #define TRUE 1
    #define FALSE 0
    
    char a;
    a = TRUE;
    if(a)
        a = FALSE;
    You use the char type instead of int to save memory.
    Not when you've got fewer than four of them in a struct or array you don't!

    Best option is to use int unless you have an array of more than say 50 or so of them. Of course if you've got more than 100000 of them then it can even make sense to pack them into 1 bit each using bitwise operations. But for one boolean, ALWAYS just use an int in C.

    Bottom line is, unless you're saving large amounts of memory by doing it, you're most likely slowing your program down unnecessarily. This kind of thing is seldom smaller AND faster.
    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"

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Or include stdbool.h which defines bool, true and false. It's IMO better than _Bool, 1 and 0.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brafil View Post
    Or include stdbool.h which defines bool, true and false. It's IMO better than _Bool, 1 and 0.
    Which is pretty much what tabstop says in post #6?

    --
    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.

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Ooops. Didn't see it while reading the other two lines he wrote.

Popular pages Recent additions subscribe to a feed