Re: Pedants, please help on integer types

Discussion in 'C Programming' started by Keith Thompson, Aug 5, 2012.

  1. Aaron W. Hsu <> writes:
    > I am hoping that the pedants from CLC can help! Basically, I am in a
    > discussion elsewhere with someone who is claiming that C90 provides for
    > no explicit means of getting an integer of *any* width at all. That is,
    > that all integer types could be any size they want. I was always under
    > the impression that the following where guarantees:
    >
    > char at least 8 bits
    > short at least 16 bits
    > long at least 32 bits
    > int at least 16 bits
    >
    > Is this not the case?


    That's essentially correct, yes.

    The standard states minimum *ranges* for the integer types. That, along
    with the requirement for a binary representation, implies the bit sizes
    you state.

    It also requires each of the types signed char, short, int, and long int
    to have a range that's a subrange of the next type in that list.

    "Plain" char may be either signed or unsigned.

    > Also, another claim is that GCC in -pedantic mode does not allow bitfields
    > on uint64_t types. Is this an actual restricton of C99 (not C90)? That is,
    > can you only use, what are they called, natural types like long long?
    > If that is the case, then does that mean that you cannot get a guaranteed
    > width size for a structure using bit fields?


    The C90 standard permits bit fields of type int, signed int, and
    unsigned int. A bit field of type int is either signed or unsigned, at
    the whim of the implementation (in other words, don't declare bit fields
    of type int); this is the only context in which "int" and "signed int"
    differ. C99 additionally permits bit fields of type _Bool.

    Bit fields may also be of some implementation-defined type.

    gcc's warning about uint64_t isn't because it's not a "natural" type.
    In fact, uint64_t is probably just a typedef for unsigned long long,
    which means they're exactly the same type. In pedantic mode, it
    probably warns about bit fields of any type other than _Bool, int,
    signed int, or unsigned int.

    > I would appreciate some authoratative advice, as I do not have a copy
    > of C90 or C99; relevant quotes from the standards would be great. I do
    > not know whether I am misremembering things or if I am correct.


    rudolf pointed you to http://flash-gordon.me.uk/ansi.c.txt, which is
    actually a draft of the 1989 ANSI C standard; the 1990 ISO C standard
    describes the same language, but added some introductory material and
    numbers the sections differently.

    The best C99 draft is

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

    which includes the offical ISO C99 standard with the three Technical
    Corrigenda incorporated into it.

    Another revision of the standard was published in 2011; the latest
    draft is at

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 5, 2012
    #1
    1. Advertising

  2. Aaron W. Hsu <> writes:
    > Keith Thompson <> writes:
    >>The C90 standard permits bit fields of type int, signed int, and
    >>unsigned int. A bit field of type int is either signed or unsigned, at
    >>the whim of the implementation (in other words, don't declare bit fields
    >>of type int); this is the only context in which "int" and "signed int"
    >>differ. C99 additionally permits bit fields of type _Bool.

    >
    > Now I am a bit confused. One cannot predict the size of int very well,
    > as it is often bigger than its minimum value. Disregarding the _Bool
    > case, and assuming that you cannot specify a bitfield size larger than
    > the size of the int, does not practically restrict you to only using
    > 16 bits of an int if you want to remain portable?


    Yes, it does.

    Noting again that you almost certainly want to use unsigned int
    rather than plain int, you cannot portably assume that bit fields
    can be wider than 16 bits.

    If you don't require absolute portability (and one of C's greatest
    strengths its ability to support system-specific code), you can
    either assume that unsigned int is wider than 16 bits, or you can
    take advantage of a given compiler's choice to support bit fields
    of type unsigned long or unsigned long long (or uint32_t et al).

    You're unlikely to find a modern hosted implementation with
    (unsigned) int narrower than 32 bits, so bit fields up to that
    width are *reasonably* portable (depening on what platforms you're
    interested in).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 5, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    598
    Big Brian
    Oct 14, 2003
  2. Replies:
    163
    Views:
    3,266
  3. jacob navia

    bunch of pedants

    jacob navia, Mar 16, 2008, in forum: C Programming
    Replies:
    65
    Views:
    1,422
    David Thompson
    Mar 31, 2008
  4. jacob navia

    Pedants

    jacob navia, Jun 21, 2008, in forum: C Programming
    Replies:
    121
    Views:
    3,055
    Richard Bos
    Jul 3, 2008
  5. rudolf

    Re: Pedants, please help on integer types

    rudolf, Aug 5, 2012, in forum: C Programming
    Replies:
    0
    Views:
    352
    rudolf
    Aug 5, 2012
Loading...

Share This Page