Question

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 way in C (not C++) to define an integer type with a specific size that ensures it will be the same across different architectures? Like:

typedef int8 <an integer with 8 bits>
typedef int16 <an integer with 16 bits>

Or any other way that will allow other parts of the program to be compiled on different architecture.

Was it helpful?

Solution

What you want is <stdint.h>, which compilers that conform to the C standard ("C99") will implement. Unfortunately, this does not include Microsoft. Fortunately, an open-source project provides a <stdint.h> for Windows, see msinttypes.

This will allow you to use int32_t and uint32_t, plus 8, 16, and 64, and many others.

Note: the header file itself is not optional in the standard, however, most of the types in the header are individually optional. Some are not. The most commonly used types are the optional ones, but nothing stops you from using the required ones. The thing is, if an implementation provides the header at all, in practice they define all of the types.

OTHER TIPS

C99, in stdint.h, defines types like int8_t and int16_t.

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 #ifdefs) 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

  1. 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.

  2. 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.

  3. 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

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.

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.

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;

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top