The most common sizes of integer types seem to be:
8 bits - signed char
16 bits - short
16 or 32 bits - int
32 or 64 bits - long
Question #1:
Does anyone know how common sizes other than these are ? I know that the
standard says that number of bits in a "byte" (char) is >= 8, but I have not
come across values other than these yet ...
Generally not for ordinary processors, from 8-bit on up to 64-bit or
so, but this is quite common for DSPs (Digital Signal Processors).
Right now I'm coding for a Texas Instruments DSP that doesn't access
memory in anything smaller than 16 bit words. CHAR_BIT is 16 on that
platform.
There are some DSPs that only deal with 32 bit quantities, so the
character types, shorts, ints, and longs are all 32 bits.
There were some early members of the Motorola 56K DSP family with
24-bit word size. IIRC, char, short and int were all 24 bits, and
long was 48 bits.
Question #2:
Is anyone aware of a package that has some portable definitions of things
like Int8, Int16 and Int32 (assuming they are possible for the platform) ?
If not, I think you could do this using the definitions in <limits.h>
(<climits> ?). I am aware that the preprocessor doesn't understand the
sizeof operator, so you can't do it that way.
Thanks,
David F
The current 1999 version of the C standard includes a header named
<stdint.h> that contains typedefs for a variety of integer types. The
required types include: int_least#_t, uint_least#_t, int_fastest#_t
and uint_fastest#_t, where # consists of at least 8, 16, 32, and 64.
The (u)int_least#_t are typedefs for the integer types occupying the
least amount of memory containing at least that many bits.
The (u)int_fastest#_t are typedefs for the integer types containing at
least that many bits that are the fastest for the processor to use, if
there is more than one type with at least that number of bits.
The types that are optional under some circumstances under C99 are:
(u)int8_t, (u)int16_t, (u)int32_t, and (u)int64_t.
These are optional because some implementations might not actually
have hardware types with exactly those widths. If a platform actually
has integer types with exactly those bit widths, if those types have
not padding bits, and if the signed versions use 2's complement
representation, then the implementation must provide the typedefs.
But this must be tailored to the compiler, that is the typedefs must
be set to the appropriate native type. Of course you only do that
once for a particular compiler, then you can include and use the
header forever.
I would suggest using these types, as they will be portable across C
compilers and almost certainly included in the next version of the C++
standard. Since almost all C++ compilers also include a C compiler,
many C++ compilers come with this header already. With the possible
exception of the 64 bit types, which is the C signed and unsigned
"long long", C++ implementations should be able to use them as well.
Here's a sample of a <stdint.h> header that should work with just
about every C and C++ compiler for the Intel x86 processors, older 16
bit compilers as well as newer 32 bit ones (with the possible
exception of the 64 bit types).
#ifndef STDINT_H_INCLUDED
#define STDINT_H_INCLUDED
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed long int32_t;
typedef unsigned long uint32_t;
typedef signed long long int64_t;
typedef unsigned long long uint64_t;
typedef signed char int_least8_t;
typedef unsigned char uint_least8_t;
typedef signed short int_least16_t;
typedef unsigned short uint_least16_t;
typedef signed long int_least32_t;
typedef unsigned long uint_least32_t;
typedef signed long long int_least64_t;
typedef unsigned long long uint_least64_t;
typedef signed char int_fast8_t;
typedef unsigned char uint_fast8_t;
typedef signed short int_fast16_t;
typedef unsigned short uint_fast16_t;
typedef signed long int_fast32_t;
typedef unsigned long uint_fast32_t;
typedef signed long long int_fast64_t;
typedef unsigned long long uint_fast64_t;
#define INT8_MAX 127
#define INT16_MAX 32767
#define INT32_MAX 2147483647
#define INT64_MAX 9223372036854775807LL
#define INT8_MIN -128
#define INT16_MIN (-INT16_MAX - 1)
#define INT32_MIN (-INT32_MAX - 1)
#define INT64_MIN (-INT64_MAX - 1)
#define UINT8_MAX 255
#define UINT16_MAX 65535
#define UINT32_MAX 4294967295
#define UINT64_MAX 18446744073709551615
#define INT_LEAST8_MAX 127
#define INT_LEAST16_MAX 32767
#define INT_LEAST32_MAX 2147483647
#define INT_LEAST64_MAX 9223372036854775807
#define INT_LEAST8_MIN -128
#define INT_LEAST16_MIN (-INT_LEAST16_MAX - 1)
#define INT_LEAST32_MIN (-INT_LEAST32_MAX - 1)
#define INT_LEAST64_MIN (-INT_LEAST64_MAX - 1)
#define UINT_LEAST8_MAX 255
#define UINT_LEAST16_MAX 65535
#define UINT_LEAST32_MAX 4294967295
#define UINT_LEAST64_MAX 18446744073709551615
#define INT_FAST8_MAX 127
#define INT_FAST16_MAX 32767
#define INT_FAST32_MAX 2147483647
#define INT_FAST64_MAX 9223372036854775807
#define INT_FAST8_MIN -128)
#define INT_FAST16_MIN (-INT_FAST16_MAX - 1)
#define INT_FAST32_MIN (-INT_FAST32_MAX - 1)
#define INT_FAST64_MIN (-INT_FAST64_MAX - 1)
#define UINT_FAST8_MAX 255
#define UINT_FAST16_MAX 65535
#define UINT_FAST32_MAX 4294967295
#define UINT_FAST64_MAX 18446744073709551615
#endif
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq