A question on bit-field widths.

Mike Lijewski lijewski at batcomputer.tn.cornell.edu
Wed Dec 19 10:56:12 AEST 1990


In article <3028 at lupine.NCD.COM> rfg at lupine.ncd.com (Ron Guilmette) writes:
>I am posting this question to both comp.std.c and comp.std.c++ because
>the question arises for both languages.
>
>Given a machine on which 'long int' is 32 bits and `short int' is 16 bits,
>what should the following program print?  Should this program even be
>allowed to compile without errors?

Bitfields are `mostly' implementation defined.  ANSI allows bitfields to
be of type 'int', 'unsigned int' and 'signed int', though an implementation
may choose to accept one of any integer type.  An implementation may impose
a maximum width on bitfields also.  So the best that can be said for the
following code is that it is implementation defined.

>
>
>struct S1 {
>	long	field:33;
>};
>
>struct S2 {
>	short	field1:17;
>	short	field2:17;
>	int	field3;
>	short	field4:17;
>};
>
>extern printf (const char *, ...);
>
>struct S1 s1;
>struct S2 s2;
>
>int main ()
>{
>  printf ("sizeof (struct S1) = %d\n", sizeof (struct S1));
>  printf ("sizeof (struct S2) = %d\n", sizeof (struct S2));
>
>  s2.field1 = 0x3FFFF;
>  s2.field2 = 0x3FFFF;
>  s2.field4 = 0x3FFFF;
>
>  printf ("field1 = %d, field2 = %d, field3 = %d\n",
>    s2.field1, s2.field2, s2.field3);
>
>  return 0;
>}
>
>As usual, it would be helpful if someone could point me at the appropriate
>parts of the C standard (or Annotated C++ Reference Manual) where such
>questions are answered.

I'm quoting from the H&S Edition 3, section 5.6.5.

>
>Thanks.


-- 
Mike Lijewski  (H)607/272-0238 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx at eagle.cnsf.cornell.edu  BITNET: mjlx at cornellf.bitnet
SMAIL:  25 Renwick Heights Road, Ithaca, NY  14850



More information about the Comp.std.c mailing list