Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!rpi!batcomputer!lijewski From: lijewski@batcomputer.tn.cornell.edu (Mike Lijewski) Newsgroups: comp.std.c Subject: Re: A question on bit-field widths. Message-ID: <1990Dec18.235612.18762@batcomputer.tn.cornell.edu> Date: 18 Dec 90 23:56:12 GMT References: <3028@lupine.NCD.COM> Organization: Cornell National Supercomputer Facility Lines: 62 In article <3028@lupine.NCD.COM> rfg@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@eagle.cnsf.cornell.edu BITNET: mjlx@cornellf.bitnet SMAIL: 25 Renwick Heights Road, Ithaca, NY 14850