-5

I thought that C interpreted true as 1 but now I'm having doubts.

Full code (compiled with GCC 5.1):

if(true && true) // Error: 'true' undeclared (first use in this function)
{
}

Why does this happen?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
cdecompilador
  • 178
  • 2
  • 9
  • To use the `true` and `false` macros in `C` (they are not reserved words), you need to include the [stdbool.h header](https://en.cppreference.com/w/c/types/boolean). – Adrian Mole Jan 18 '20 at 21:36
  • @tadman yes, but both are integers and because of that comparable or not? – cdecompilador Jan 18 '20 at 21:36
  • Only if you give the compiler a hint, as in include the right header file. – tadman Jan 18 '20 at 21:37
  • 3
    @tadman It does have a boolean type. It's called `_Bool`. We need macros to rename it to `bool` because C *used to* not have a boolean type and people *used to* write macros to fake it, so all the `bool`-things are made opt-in so collisions are avoided. – HTNW Jan 18 '20 at 21:39

1 Answers1

2

true is not a keyword in C like it is in C++.

To access it you'll have to #include <stdbool.h>.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76