Defining a bool type using typedef

suggest change

Considering that most debuggers are not aware of #define macros, but can check enum constants, it may be desirable to do something like this:

#if __STDC_VERSION__ < 199900L
typedef enum { false, true } bool;
/* Modern C code might expect these to be macros. */
# ifndef bool
#  define bool bool
# endif
# ifndef true
#  define true true
# endif
# ifndef false
#  define false false
# endif
#else
# include <stdbool.h>
#endif

/* Somewhere later in the code ... */
bool b = true;

This allows compilers for historic versions of C to function, but remains forward compatible if the code is compiled with a modern C compiler.

For more information on typedef, see https://stackoverflow.com/documentation/c/2681/typedef, for more on enum see https://stackoverflow.com/documentation/c/5460/enumerations

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents