about Boolean

N

NumLockOff

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
 
F

FrancisC

thx a lot!

NumLockOff said:
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
 
T

Tristan Miller

Greetings.

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
 
P

pete

FrancisC said:
thx a lot!

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.
 
T

those who know me have no need of my name

in comp.lang.c i read:
#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

Arthur J. O'Dwyer

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
 
K

Keith Thompson

FrancisC said:
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.
 
P

pete

Arthur said:
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) */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top