A question on bit-field widths.

Ron Guilmette rfg at lupine.ncd.com
Tue Dec 18 09:10:36 AEST 1990


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?


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.

Thanks.



More information about the Comp.std.c mailing list