Boolean Types in C

This is a discussion on Boolean Types in C within the C Programming forums, part of the General Programming Boards category; I am coming from the C++ student world and must do a simple program in C. I have to make ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Boolean Types in C

    I am coming from the C++ student world and must do a simple program in C. I have to make a Boolean Flag. Is there a Boolean type in C or do I have to typedef it? I did a search but came up with no awnsers and my SAMS book does not even mention it.

  2. #2
    Unregistered
    Guest
    This might work. You can use bitfields in C to declare how many bits you want a variable to hold. For example:
    struct boolean
    {
    unsigned boolean :1;
    };
    typedef struct boolean BOOLEAN
    creates a variable that holds 1 bit or 2 values. These values can be yes or no if you use them like this.
    #define YES 1
    #define NO 0

    boolean=NO;

    I don't know for sure though. Its late. Just a suggestion. Good luck.

  3. #3
    Unregistered
    Guest
    You also actually have to declare a variable of your type. Like this:

    BOOLEAN boolean;

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    IN MSVC 5 and greater there is bool type

    If not

    #define BOOL int
    #define TRUE 1
    #define FALSE 0
    #define INVALID -1
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Borland

    For Borland users:

    bool bVar;
    bVar=true;
    if(bVar==true) bVar=false;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    This is the easiest way I have found to make a boolean type in C:

    Code:
    typedef int boolean;
    enum boolean {false, true};
    After you've typedef'ed it, you can use it in your program where ever you want to use a boolean, without having to remember to pass 1 for true and 0 for false (it already knows this since its enumerated).

    Worth a shot, always works for me (in UNIX)
    DrakkenKorin

    Get off my Intarweb!!!!

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Thanks!!!

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I just always use:
    Code:
    int yesno;
    
    if (/*** CODE ***/)
        yesno = 0;
    
    else
        yesno  = 1;
    Works for me. I don't need a "boolean" type. Just a flexible int.

    --Garfield
    1978 Silver Anniversary Corvette

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    also...

    Using bitwise operators you can store up to 8 boolean expressions in one char.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yeah, that is good for optimization. Each bit is a switch.

    --Garfield
    1978 Silver Anniversary Corvette

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!

    There isnīt any boolean data-type in C.

    klausi
    When I close my eyes nobody can see me...

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    hm?

    Not? Well, you can still use the method I mentioned in my reply before this one...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Sayeh
    Guest
    This is one of the reasons you create a header file of all the types you have created. You can include it in every project you write.

    === custTypes.h ===============

    #ifndef __custTypes__
    #define __custTypes__


    #ifndef boolean
    typedef int boolean;

    enum boolean {false, true};
    #endif boolean

    #endif __custTypes__


    === custTypes.h ===============

    enjoy.

  14. #14
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    There isnīt any boolean data-type in C
    C99 supports the use of _Bool and also bool (if stdbool.h is included), although hardly any existing compilers will allow it.
    zen

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Yes, but when I talk about something like that I mostly talk about the ANSI-standard.

    klausi
    When I close my eyes nobody can see me...

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

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 11:57 AM
  2. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  3. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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