Visual Studio 2010 and stdbool.h

This is a discussion on Visual Studio 2010 and stdbool.h within the C Programming forums, part of the General Programming Boards category; I'm pretty sure this is again something about C99, but how do I use booleans in C code in Visual ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    19

    Visual Studio 2010 and stdbool.h

    I'm pretty sure this is again something about C99, but how do I use booleans in C code in Visual Studio 2010?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    typedef int bool;

    Or something similar.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Why not just
    typedef char bool;
    ?

    Why use 4 bytes (possibly) when only 1 is required? I never understood why int is used for bools.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    It gives me
    IntelliSense: invalid combination of type specifiers
    error C2065: 'false' : undeclared identifier
    error C2065: 'true' : undeclared identifier

    What I find strange is that it has "bool", "true" and "false" all blue, ie. it understands those are keywords, but doesn't know how to use them.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    That's because they are keywords in C++. But they are not for C, so you have to define them.
    Something like
    Code:
    #ifndef __cplusplus
    #define TRUE 1
    #define FALSE 0
    #endif
    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.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you were using some C99 compliant compiler you could use the framework for booleans in stdbool.h . Sadly though, that is not the case with VS.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    If you were using C++ as a better C, you wouldn't have to deal with the problems either. Or even better if you actually used C++.
    But *shrug*. MS has declared that there just aren't enough C customers to warrant implementing C99.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by Elysia View Post
    If you were using C++ as a better C, you wouldn't have to deal with the problems either. Or even better if you actually used C++.
    But *shrug*. MS has declared that there just aren't enough C customers to warrant implementing C99.
    C++ is too advanced for the embedded systems we're using. It has to be C.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    o_O
    But then again, Visual Studio's C compiler doesn't for for embedded systems, does it?
    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.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by Elysia View Post
    o_O
    But then again, Visual Studio's C compiler doesn't for for embedded systems, does it?
    It's good enough for me if I can just write and test the code with Visual Studio. It's ok if the system knows absolutely nothing about Visual Studio.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by Elysia View Post
    That's because they are keywords in C++. But they are not for C, so you have to define them.
    Something like
    Code:
    #ifndef __cplusplus
    #define TRUE 1
    #define FALSE 0
    #endif
    Yeah, this works. Thanks!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Quote Originally Posted by Jaymond Flurrie View Post
    It's good enough for me if I can just write and test the code with Visual Studio. It's ok if the system knows absolutely nothing about Visual Studio.
    So basically the program that is supposed to run on the embedded system can also run on Windows, I take it? And that is why you use VS to test the code on Windows before porting it to the embedded. I see.
    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.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by Elysia View Post
    So basically the program that is supposed to run on the embedded system can also run on Windows, I take it? And that is why you use VS to test the code on Windows before porting it to the embedded. I see.
    It can run on Windows, yes, but the huge reason why I use VS2010 here is
    1) I happen to have VS2010
    2) The thing that I'm trying to do here is to just learn the C as well as possible



    One question, or thing that I'm looking reassurance for, is that if I use this "original C" that VS2010 uses, can I assume anything that can run C99 can run this "original" C as well just "as is"?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    No, you cannot. C99 is not entirely backwards compatible with C90.
    The only thing that springs to mind, however, is the implicit return type. It's deprecated in C90 and disallowed in C99.
    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.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by Elysia View Post
    No, you cannot. C99 is not entirely backwards compatible with C90.
    The only thing that springs to mind, however, is the implicit return type. It's deprecated in C90 and disallowed in C99.
    Ok, I'll keep that in my mind. Thanks for the heads up!

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

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