Re: Specific sizes of variable types



"gamehack" <gamehack@xxxxxxxxx> writes:
> I'm writing an application which will be opening binary files with
> definite sizes for integers(2 byte integers and 4 byte longs). Is there
> a portable way in being sure that each integer will be exactly 2 bytes?
> Should I have a platform dependent header file which checks for
> CHAR_BIT and sizeof(int) etc and have a few typedefs like int16, long32
> etc.
> <OT>
> If I have to go non-portable, then what's the way to determine the
> sizes before compilation and typedef accordingly?
> </OT>

C99 has a standard header called <stdint.h>, which provides typedefs
for int8_t, uint8_t, int16_t, uint16_t, int32_t, etc. Those are
exact-width types, and the signed ones are required to be
2's-complement with no padding bits; they may not exist if the
implementation doesn't provide types with the required attributes. It
also provides "least" types (the smallest type with at least the
specified number of bits) and "fast" types (the "fastest" type with at
least the specified number of bits); it's not always clear what
"fastest" really means.

If your implementation doesn't provide this header, you can roll your
own, or you can use Doug Gwyn's C90-compatible public-domain
implementation q8 at <http://www.lysator.liu.se/c/q8/>. You'll
probably need to do some tailoring for your system.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.



Relevant Pages

  • Re: Specific sizes of variable types
    ... I'm writing an application which will be opening binary files with ... definite sizes for integers(2 byte integers and 4 byte longs). ... CHAR_BIT and sizeofetc and have a few typedefs like int16, ...
    (comp.lang.c)
  • Specific sizes of variable types
    ... I'm writing an application which will be opening binary files with ... definite sizes for integers(2 byte integers and 4 byte longs). ... CHAR_BIT and sizeofetc and have a few typedefs like int16, ...
    (comp.lang.c)
  • Re: Splitting huge XML Files into fixsized wellformed parts
    ... I am kind of depressed :-) I want to split xml-files with sizes ... copy of the header from the ... <OfferInfo> ...
    (comp.lang.awk)
  • Splitting huge XML Files into fixsized wellformed parts
    ... I am kind of depressed :-) I want to split xml-files with sizes ... copy of the header from the ... <OfferInfo> ...
    (comp.lang.awk)
  • Re: reporting typedef errors automatically
    ... The header provides minimum and maximum macros for the ... But if the types you need are there, why do you care what their sizes ... home brew for a compiler that doesn't come with one. ...
    (comp.lang.c)