Re: bool variable.(a non-standard question)



"Peter Nilsson" <airia@xxxxxxxxxxx> writes:
Richard Heathfield wrote:
... Of course, apart from a bitfield (of type unsigned int and
width 1), there is no [C90] type that can /only/ take a value from the range
[0, 1], and a bitfield would be unlikely to win any awards for speed.

If speed is at a premium, he should choose int:

typedef int bool;

I prefer...

typedef unsigned int bool;

Because you can then do robust boolean bitfields...

struct blah
{
bool flag_1 : 1;
bool flag_2 : 1;
bool flag_3 : 1;
...
};

Since C99 uses the name "bool", I wouldn't use that identifier in C90
code. It won't cause any problems for the compiler, since C99
carefully added a new keyword _Bool and declared "bool" only in the
new <stdbool.h> header, but it could still cause confusion for the
reader.

And I'd prefer to use "unsigned int" directly for bit fields:

struct blah {
unsigned flag_1 : 1;
unsigned flag_2 : 1;
unsigned flag_3 : 1;
...
};

since I know that int, signed int, and unsigned int are the only
portably legal types for bit fields in C90.

On the other hand, I suppose a case could be made for using "bool" if
you're trying to be compatible with both C90 and C99.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages