Somacon.com: Articles on web development, software, and hardware
§ Home > Index > C and C# Programming

C++ Integer Data Types

These are some reference notes for using integer data types in C/C++.

The idea of C++ is for it to work across many systems, but allow optimization to particular systems. This is why there are so many integer data types, and why they are not given a fixed and definite size. For those needing more definite, portable sizes, refer to <boost/cstdint.hpp>, which should eventually become part of the C++ standard.

Integer Data Types

TypeVariations
char unsigned char, signed char
short unsigned short, signed short
int unsigned int, signed int, short int, long int
long unsigned long, signed long

Synonyms

Some integer data types can be expressed in shorthand.

Sizes

The bit-width of a char is guaranteed to be at least eight bits. The actual bit-width is an unspecified, system-dependent unit (called a byte) that can represent the implementation's character set. All the other type sizes are expressed by relation to the size of char (or byte), with certain minimum guarantees.

char  : at least 8 bits  : equal to one unit
short : at least 16 bits : at least as wide as char
int   : at least 16 bits : at least as wide as short
long  : at least 32 bits : at least as wide as int

According to these rules, each of the four data types could be the same size on a particular system, as long as it is at least 32 bits. Plain, signed, and unsigned variations of a particular type are the same size. Your C++ compiler may also support long long, that is guaranteed to be at least 64-bits in the C99 standard (that's a C standard, not a C++ standard). See Visual C++ <limits> header file to get the ranges on your system.

Recommendation

For new programs, it is recommended that one use only bool, char, int, and double, until circumstance arises that one of the other types is needed.

Links


Created 2004-11-17, Last Modified 2012-06-25, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.