Search code examples
cvisual-studio-2005booleankeywordc89

Is "true" (lower case) already a keyword in visual studio? Can it be overridden?


I am using the visual studio 2005 C++ compiler (but are coding using C89 type ANSI C) and am unable to define a "true" and "false" keyword to use as follows:

#define true 1
#define false 0

I have used this exact code successfully using the Green Hills C++ compiler, but visual studio behaves as if "true" and "false" are already keywords. Is this true? Can I override them with my own definition, and if so how?

With using the lines above I get C2143 errors during compilation for lines such as:

someFuntion(someVar,otherVar,true);

I have already made certain the other types match the function definition and if I change the true to a "1" or "0x1" the errors go away.


Solution

  • First, true and false are never a C keyword, not even in C99/C11. C99 introduced native boolean type _Bool but true and false are still just macros for 1 and 0 respectively defined in stdbool.h

    There's no reference that Visual Studio defines the macro true or false, but you can always use this:

    #ifndef true
    #define true 1
    #endif
    #ifndef false
    #define false 0
    #endif