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
Type | Variations |
---|---|
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.
long int
is the same aslong
short
is the same asshort int
unsigned
is the same asunsigned int
signed
is the same assigned int
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
- Integer Types In C and C++, Jack Klein, 2000, http://www.jk-technology.com/c/inttypes.html
- comp.lang.c C FAQ
- http://www.parashift.com/c++-faq-lite/ - comp.lang.c++ C++ FAQ
- alt.comp.lang.learn.c-c++ FAQ
- Bjarne Stroustrup, The C++ Programming Language, Addison-Wesley Professional, Reading, Massachusetts, 2000.
- Fundamental Types of the C++ Language, C++ Language Reference, MSDN Library.
Disclaimer: This content is provided as-is. The information may be incorrect.