I am aware that the specification of the C language does not dictate the exact size of each integer type (e.g., int
).
What I am wondering is: Is there a
You could always write an arithmetic library that used vectors of unsigned char for the numbers. That way you could use numbers of whatever bit length you want, and even allow the bit length to vary.
Actually, you don't need to implement such a library because GNU MP handles this already.
http://gmplib.org/
You might want to take a look at pstdint.h. It is a portable implementation of stdint.h, and does not require C99 compiler support.
No, the C standard specifies minimum sizes for integral types but makes no guarantee on maximum sizes.
An implementation shall provide intN_t
types if types of that size are available. I only mention that since you had a cross-platform tag - an implementation that does not have a type of the correct bit width does not need to provide those types.
You can generally select (with setting defines with, for example, cc -D_INT16_IS_INT
and #ifdef
s) the correct type to use for a specific bit size. You can work out the required defines for each platform you want to support with C code using CHAR_BIT
and sizeof()
.
The relevant section of the c1x draft (n1362) is:
7.18.1.1 Exact-width integer types
The typedef name intN_t
designates a signed integer type with width N
, no padding bits, and a two’s complement representation. Thus, int8_t
denotes a signed integer type with a width of exactly 8 bits.
The typedef name uintN_t
designates an unsigned integer type with width N
. Thus, uint24_t
denotes an unsigned integer type with a width of exactly 24 bits.
These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.
Regarding the selection of types, something like this should suffice:
#ifdef INT32_IS_SHORT
typedef short INT32
#endif
#ifdef INT32_IS_INT
typedef int INT32
#endif
#ifdef INT32_IS_LONG
typedef long INT32
#endif
C99, in stdint.h, defines types like int8_t
and int16_t
.
Unless you check for each platform with #ifdef
or so, I doubt it's easily possible. But many libraries already do that task for you. For MSVC it's __int8, __int16, &c. The GTK library has similar typedefs.
As far as I know, the answer is no. We code for different platforms and we just use typedefs for the specific platforms using #if/#else. For example on Win32: typedef int int32;