Home:ALL Converter>Size of int in C on different architectures

Size of int in C on different architectures

Ask Time:2009-10-23T14:45:07         Author:NawaMan

Json Formatter

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.

Author:NawaMan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1611733/size-of-int-in-c-on-different-architectures
DigitalRoss :

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.\n\nThis will allow you to use int32_t and uint32_t, plus 8, 16, and 64, and many others.\n\nNote: 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.",
2009-10-23T06:52:18
Martin v. Löwis :

C99, in stdint.h, defines types like int8_t and int16_t.",
2009-10-23T06:47:52
paxdiablo :

No, the C standard specifies minimum sizes for integral types but makes no guarantee on maximum sizes.\n\nAn 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.\n\nYou 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().\n\nThe relevant section of the c1x draft (n1362) is:\n\n\n\n7.18.1.1 Exact-width integer types\n\n\nThe 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.\nThe 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.\nThese 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.\n\n\n\n\nRegarding the selection of types, something like this should suffice:\n\n#ifdef INT32_IS_SHORT\n typedef short INT32\n#endif\n#ifdef INT32_IS_INT\n typedef int INT32\n#endif\n#ifdef INT32_IS_LONG\n typedef long INT32\n#endif\n",
2009-10-23T06:49:34
Joey :

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.",
2009-10-23T06:48:09
Marcin Koziuk :

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.",
2009-10-23T11:38:47
Sebastiaan M :

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;",
2009-10-23T06:49:30
yy