Navigation



A.6 — Fixed-width integers

Fixed width integers

The C99 standard defines a set of integer types that have predetermined sizes, so you don’t have to worry about the size of an integer changing if you move to another platform.

The fixed width integer types defined in C99 are named as follows:

Name Type
int8_t 1 byte signed
uint8_t 1 byte unsigned
int16_t 2 byte signed
uint16_t 2 byte unsigned
int32_t 4 byte signed
uint32_t 4 byte unsigned
int64_t 8 byte signed
uint64_t 8 byte unsigned

Unfortunately, C++ does not define or provide a standardized way to access these!

However, many modern C++ compilers do provide access to these, typically by including stdint.h. Visual Studio 2005 and 2008 do not include stdint.h, but 2010 does. If you are using the boost library, boost provides these as part of <boost/cstdint.hpp>.

If your compiler does not support these types, the good news is that you can download Paul Hsieh’s pstdint.h cross-platform compatible version of the stdint.h header. Simply include the pstdint.h file in your project and it will define the fixed width integer types with the proper sizes for your platform.

We don’t use the fixed width integers in this tutorial series because they aren’t officially part of C++, but I do recommend using them on your own, especially if you are developing a cross-platform compatible application. There’s simply no reason not to, especially when pstdint.h can easily be dropped in or distributed with your code!

As a side note: some compilers define their own version of fixed width integers — for example, Visual Studio defines __int8, __int16, etc… You should avoid these like the plague.

B.1 — Introduction to C++11
Index
A.5 — Debugging your program (watching variables and the call stack)

4 comments to A.6 — Fixed-width integers

You must be logged in to post a comment.