Y
Yoshi
Prior to C99, C didn't have a boolean type. A lot of code defined its
own type, often using the name "bool", so when the C99 committee
decided to add a boolean type, they couldn't make "bool" a keyword
without breaking existing code. So they introduced the keyword
"_Bool", but they also created the new <stdbool.h> header which
defines "bool" as an alias for "_Bool". (This doesn't break existing
code, since no existing code would have "#include <stdbool.h>".)
But note that both _Bool and bool are C99-specific, and some compilers
might not support it.
very interesting story, then actually we would better not use _Bool
after all.
In any case, a bit field declared as
some_type bf:1;
will only occupy 1 bit, whatever some_type is. That's the whole
point.
Let me ask this point. since below code shows different size according
to each types(_Bool, unsigned short, unsigned int). so does it really
consume only 1 bit? I think I'm missing something..
It looks like each bit field consumes only 1 bit, but other bits are
not used in case of unsigned short and unsigned int.
#include <stdio.h>
typedef struct _mychar_b{
_Bool seven: 1;
_Bool six: 1;
_Bool five: 1;
_Bool four: 1;
_Bool three: 1;
_Bool two: 1;
_Bool one: 1;
_Bool zero: 1;
}mychar_b;
typedef struct _mychar_short{
unsigned short seven: 1;
unsigned short six: 1;
unsigned short five: 1;
unsigned short four: 1;
unsigned short three: 1;
unsigned short two: 1;
unsigned short one: 1;
unsigned short zero: 1;
}mychar_short;
typedef struct _mychar_int{
unsigned int seven: 1;
unsigned int six: 1;
unsigned int five: 1;
unsigned int four: 1;
unsigned int three: 1;
unsigned int two: 1;
unsigned int one: 1;
unsigned int zero: 1;
}mychar_int;
int main(void)
{
printf("sizeof mychar_b = %d\n",sizeof(mychar_b));
printf("sizeof mychar_short= %d\n",sizeof(mychar_short));
printf("sizeof mychar_int = %d\n",sizeof(mychar_int));
return 0;
}
Output is...
sizeof mychar_b = 1
sizeof mychar_short= 2
sizeof mychar_int = 4
If I wanted a 1-bit bit field, I'd use unsigned int:
unsigned int bf:1;
(A plain int bit field can be either signed or unsigned; that's an
annoyingly odd historical glitch in the language.)
The current official standard for the C language consist of the 1999
ISO C standard plus the three Technical Corrigenda. They've been
conveniently merged into a not-quite-but-almost-official document,
<http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf>.
Support for the C99 standard is not yet universal. The C90 standard
is still in wide use. I don't know of a good free source for it.
Note that the standard documents are quite dense; they're definitely
not tutorials. A good tutorial, arguably the best, is "The C
Programming Language", 2nd Edition, by Kernighan & Ritchie (K&R2). It
covers C90, not C99, but that's not much of a problem. A good
reference book is "C: A Reference Manual", 5th Edition, by Harbison &
Steele (H&S5). The comp.lang.c FAQ, <http://www.c-faq.com/>, is
another excellent resource; it also includes a section on other
resources, including some books that I've neglected to mention here.
Thank you for valuable information, I'm being overwhelmed to find that
pdf file is over 550pages...but I think I should get H&S5 anyway.
That's awesome, so I should not use bit fields for the case I need to[...]
OK, thanks for telling me that, though I want to see the reference...
Any decent reference should explain how bit fields are defined. The
real point here, though, is the *lack* of any reference specifying how
bit fields are laid out (though you might find documents that describe
how they're handled by a particular compiler).
expect certain layout of bit fields. But how inconvenient!