"FrancisC" <fr**********@h ong-kong.crosswinds .net> writes:
how to define Boolean in C ?
boolean abc; ???
is it default as True or False??
C90 has no builtin boolean type; see section 9 of the C FAQ
at <http://www.eskimo.com/~scs/C-faq/top.html>.
For C99, the type _Bool is builtin, and the header <stdbool.h> defines
macros for "bool", "true", and "false".
There are a number of ways to define a boolean type and the boolean
true/false values in C90. Trickery like
#define false 0
#define true (!false)
is unnnecessary; something simple like
#define false 0
#define true 1
is much clearer.
Never compare a value to true or false. If you have a boolean value,
just use it as one. For example:
int ok; /* or bool ok; */
...
if (ok) /* good */
if (!ok) /* good */
if (ok == false) /* needlessly verbose */
if (ok == true) /* needlessly verbose and potentially dangerous */
If you think (ok == false) is clearer than the equivalent (!ok),
you should think that ((ok == false) == true) is even clearer.
(ok == true) is even worse. A condition is true if the value of
the expression is any non-zero value. If the value of ok happens to
be 2, (ok) and (ok == true) are different. The builtin relational
operators will always yield 0 or 1, but there are other ways to get
boolean values.
Note that if you're using the value of a pointer as a condition,
using a (strictly redundant) comparison to NULL is acceptable:
int *ptr;
...
if (ptr) /* ok */
if (ptr != NULL) /* ok */
This is a matter of style. I personally prefer to make the comparison
explicit, but plenty of good C programmers feel differently. If you're
going to be reading other people's C code, you'll need to understand
both idioms.
--
Keith Thompson (The_Other_Keit h)
ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"