Re: Specific sizes of variable types
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sat, 21 Jan 2006 22:29:07 GMT
"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.
.
- Follow-Ups:
- Re: Specific sizes of variable types
- From: gamehack
- Re: Specific sizes of variable types
- References:
- Specific sizes of variable types
- From: gamehack
- Specific sizes of variable types
- Prev by Date: Re: Specific sizes of variable types
- Next by Date: Re: Specific sizes of variable types
- Previous by thread: Re: Specific sizes of variable types
- Next by thread: Re: Specific sizes of variable types
- Index(es):
Relevant Pages
|
|