Yevgen Muntyan said:
Keith Thompson wrote: [...]
But it's easy enough to use <stdbool.h> conditionally.
For example:
#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
typedef enum { false, true } bool;
#endif
This "bool" typedef doesn't have exactly the same semantics as C99's
bool/_Bool type, but it's usable if you avoid writing writing
problematic code.
The following code doesn't compile with "gcc foo.c":
#include <stdbool.h>
#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
typedef enum { false, true } bool;
#endif
int main (void)
{
return 0;
}
You added an extra "#include <stdbool.h>" outside the #if block. In
C99 mode, <stdbool.h> is included twice, which is harmless. In
non-C99 mode, it's included once, defining "false", "true", and "bool"
as macros, making the typedef illegal.
Remove the extra #include and it's fine.
I guess gcc doesn't define that __STDC_VERSION__ by default
(and it's not a bug, right?). In particular, you can't put
that into a public header, and you need to be careful with
your compiler if you use this code.
The code is designed to work whether __STDC_VERSION__ is defined or
not. Any undefined symbols in a #if condition are replaced with 0.
But what's the point anyway? Using int as boolean type, and TRUE
and FALSE work well enough, and traditional
#ifndef TRUE
#define TRUE 1 /* or !0 or !FALSE or whatever you like */
#endif
doesn't break, does it? (Problems of Dumbos which write dimbo code
do not count, they will also do
#define false -1
anyway).
Sure, that works too. The point is to be consistent with C99 without
requiring a C99 implementation. If (yes, it's big if) C99 becomes as
widespread as C90 is now, the code can be simplified to use
<stdbool.h> unconditionally. If you define macros "TRUE" and "FALSE",
and "true" and "false" are part of the language, then you'll have an
ugly mixture of different ways of doing the same thing.