"Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> writes:[color=blue]
> I was just thumbing through K&R 2nd Edition and thought I'd read up on
> bit-fields, which I [personally] haven't had much use for in my
> career. Anyhow, K&R says that, "Fields may be declared only as ints;
> for portability, specify signed or unsigned explicitly". So it got me
> wondering what would happen if I tried to declare a field as a short
> rather than an int.[/color]
What will happen depends on the implementation. The standard says
(C99 6.7.2.1p4):
A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other
implementation-defined type.
An implementation may support other types for bit fields, but it's not
required to; any program that uses such an extension is non-portable.
[color=blue]
> I compiled my code with GCC 3.3.6 with all warning turned on and I got
> no warnings returned. Have things changed since K&R or is there
> something bad about my code?
>
> On my implementation, the sizeof() each bit-field is identical to the
> type I used to declare the fields.[/color]
C99 6.5.3.4p1:
The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of
such a type, or to an expression that designates a bit-field
member.
This is a constraint, so if gcc doesn't give you a diagnostic, it's
non-conforming. (gcc can generally be persuaded to be conforming, or
nearly so, with command-line options.)
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <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.