469,579 Members | 1,693 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

about Boolean

how to define Boolean in C ?

boolean abc; ???

is it default as True or False??



Nov 13 '05 #1
8 43726
typedef unsigned int boolean;

#define false 0
#define true (!false)

if the variable is in a global scope, it will be initialized to 0, i.e.
false.

if it is an auto, i.e. on the stack, the result will be undefined.

e.g.

typedef unsigned int boolean;

#define false 0
#define true (!false)

boolean fGlobal; // automatically initialized to 0, i.e. false

int main(void)
{
boolean fLocal; // uninitialized. could be 0 or non zero
}

In C, 0 is considered to be false and anything nonzero is considered true.

Hope that helps

"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote in message
news:bm**********@news.hgc.com.hk...
how to define Boolean in C ?

boolean abc; ???

is it default as True or False??




Nov 13 '05 #2
thx a lot!

"NumLockOff" <nu********@hotmail.com> 在郵件
news:zt********************@comcast.com 中撰寫...
typedef unsigned int boolean;

#define false 0
#define true (!false)

if the variable is in a global scope, it will be initialized to 0, i.e.
false.

if it is an auto, i.e. on the stack, the result will be undefined.

e.g.

typedef unsigned int boolean;

#define false 0
#define true (!false)

boolean fGlobal; // automatically initialized to 0, i.e. false

int main(void)
{
boolean fLocal; // uninitialized. could be 0 or non zero
}

In C, 0 is considered to be false and anything nonzero is considered true.

Hope that helps

"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote in message
news:bm**********@news.hgc.com.hk...
how to define Boolean in C ?

boolean abc; ???

is it default as True or False??





Nov 13 '05 #3
Greetings.

In article <bm**********@news.hgc.com.hk>, FrancisC wrote:
how to define Boolean in C ?

boolean abc; ???
#include <stdbool.h>

....

_Bool abc;
is it default as True or False??


Neither. Local variables are uninitialized by default, and undefined
behaviour can result from using them before a value is assigned. Global
variables of type _Bool will be initialized to 0.

Note that this answer applies to the latest version of C, C99, which may or
may not be supported by your compiler. For C89, you'll need to use some
integer type.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #4
FrancisC wrote:

thx a lot!

"NumLockOff" <nu********@hotmail.com> 在郵件
news:zt********************@comcast.com 中撰寫...
typedef unsigned int boolean;

#define false 0
#define true (!false) In C, 0 is considered to be false
and anything nonzero is considered true.


For tests,
(x == false) and (x != false) will work for any integer x.

(x == true) and (x != true) will only work right
if you constrain x to be either 0 or 1.
Though, it would also work right if you consider 1 to be true,
while all other values are false, which would be unusual code.

--
pete
Nov 13 '05 #5
in comp.lang.c i read:
In article <bm**********@news.hgc.com.hk>, FrancisC wrote:
how to define Boolean in C ?

#include <stdbool.h> _Bool abc;


it is not necessary to include <stdbool.h> to make use of _Bool, which is a
basic type in c99 conformant compilers.

<stdbool.h> provides several macros, among which is `bool' which expands to
`_Bool', so that you can write instead:

bool abc;

--
a signature
Nov 13 '05 #6

On Fri, 10 Oct 2003, pete wrote:
"NumLockOff" <nu********@hotmail.com> [wrote:]

#define false 0
#define true (!false)


For tests,
(x == false) and (x != false) will work for any integer x.

(x == true) and (x != true) will only work right
if you constrain x to be either 0 or 1.


Just for fun...

[DISCLAIMER: I urge everyone *NOT* to use this code. It's bad.]

#define FALSE 0
#define TRUE 0==0

Are there any common-sense constructs for which *this* set of
#defines produces crazy results? -- Note that now, even if some
brain-dead individual were ever to write

if (x == true) ...

the code would still work as if he'd written

if (x != false) ...
But remember: The above is REALLY BAD and WILL BITE YOU if you
use it, so DON'T. Just write

if (x) ...

-Arthur
Nov 13 '05 #7
"FrancisC" <fr**********@hong-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_Keith) 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"
Nov 13 '05 #8
Arthur J. O'Dwyer wrote:

On Fri, 10 Oct 2003, pete wrote:
"NumLockOff" <nu********@hotmail.com> [wrote:]
>
> #define false 0
> #define true (!false)


For tests,
(x == false) and (x != false) will work for any integer x.

(x == true) and (x != true) will only work right
if you constrain x to be either 0 or 1.


Just for fun...

[DISCLAIMER: I urge everyone *NOT* to use this code. It's bad.]

#define FALSE 0
#define TRUE 0==0

Are there any common-sense constructs for which *this* set of
#defines produces crazy results? -- Note that now, even if some
brain-dead individual were ever to write

if (x == true) ...


if (TRUE == x) ... /* same as (1 == x) */

--
pete
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Zunbeltz Izaola | last post: by
22 posts views Thread by James H. | last post: by
12 posts views Thread by SStory | last post: by
1 post views Thread by UJ | last post: by
90 posts views Thread by John Salerno | last post: by
8 posts views Thread by Tony Johansson | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.