F
FrancisC
how to define Boolean in C ?
boolean abc; ???
is it default as True or False??
boolean abc; ???
is it default as True or False??
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
how to define Boolean in C ?
boolean abc; ???
is it default as True or False??
FrancisC said:thx a lot!
#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;
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.
FrancisC said:how to define Boolean in C ?
boolean abc; ???
is it default as True or False??
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) ...
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.