0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • The problem was not with the definition but with a syntax error. I had incorrectly used the following: #define true 1; #define false 0; The semicolons caused the problem. If you would like to expound upon why I will up vote your answer. – bobthearsonist Aug 22 '12 at 15:54

1 Answers1

1

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
Yu Hao
  • 119,891
  • 44
  • 235
  • 294