Thread: Using bool with MSVC++

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    Using bool with MSVC++

    Hi,

    I am trying to include stdbool.h in a C program I am writing using MSVC++. I get a fatal error that it cannot open. No such file exists. I thought I needed to place it in the "stdafx.h" but that did not work either.

    What is my issue here?
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That stdbool.h is a C99 header, and Microsoft will never support C99.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Thank you Salem.

    Is there any way to use boolean at all...another header...anything?

    Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A couple of #defines (or an enum if you're feeling that way inclined), and any integral type work as a boolean in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Lost me.
    do you mean

    #ifndef FALSE
    #define FALSE 0
    #endif

    #ifndef TRUE
    #define TRUE 1
    #endif

    typedef int bool

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    I am pretty new to this

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That would work, though I'm unusure of what stdbool.h contains.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > do you mean
    Yes, that's exactly it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe something like this would be better, in case you ever plan on updating the code to C++:

    Code:
    #ifndef __cplusplus
        typedef int bool;
    #   define true  1
    #   define false 0
    #endif

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    thanks salem
    and cpjust

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Better to use an enum. It will give you better error messages:
    Code:
    #ifndef __cplusplus
    enum bool{
        false,
        true
    };
    #endif
    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.

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