Basic built-in types
C + + provides programmers with a rich range of built-in data types and user-defined data types. The following table lists seven basic C + + data types:
type |
Key Words |
Boolean type |
bool |
Character type |
Char |
Integral type |
Int |
Floating point Type |
Float |
Double floating point type |
Double |
No type |
void |
Wide character type |
wchar_t |
Some basic types can be decorated with one or more type modifiers:
- Signed
- Unsigned
- Short
- Long
The following table shows the memory that is required for various variable types to store values in memory, and the maximum and minimum values that can be stored by variables of that type.
Note: different systems will vary.
1#include <iostream>2 3 using namespacestd;4 5 intMain ()6 {7 //Print the maximum value of type int8cout <<"int type maximum:"<< Int_max <<Endl;9 //Print the minimum value of type intTencout <<"type of int minimum value:"<< Int_min <<Endl; One //Print the number of bytes that int occupies Acout <<"int type byte number:"<<sizeof(int); - return 0; -}
type |
Number of bits (bytes) |
Range |
Char |
8-bit, 1-byte |
128 to 127 or 0 to 255 |
unsigned char |
8-bit, 1-byte |
0 to 255 |
Signed Char |
8-bit, 1-byte |
128 to 127 |
Int |
32-bit, 4-byte |
2147483648 to 2147483647 |
unsigned int |
32-bit, 4-byte |
0 to 4294967295 |
Signed int |
32-bit, 4-byte |
2147483648 to 2147483647 |
Short int |
16-bit, 2-byte |
32768 to 32767 |
unsigned short int |
16-bit, 2-byte |
0 to 65,535 |
Signed Short int |
16-bit, 2-byte |
32768 to 32767 |
Long int |
64-bit, 8-byte |
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Signed Long int |
64-bit, 8-byte |
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int |
64-bit, 8-byte |
0 to 18,446,744,073,709,551,615 |
Float |
32-bit, 4-byte |
+/-3.4e +/-38 (up to a number) |
Double |
32-bit, 8-byte |
+/-1.7e +/-308 (~15 digits) |
Long double |
128-bit, 16-byte |
+/-1.7e +/-308 (~15 digits) |
wchar_t |
2 or 4 bytes |
1 wide characters |
As you can tell from the table above, the size of the variable varies depending on the compiler and the computer you are using.
C + + Common data types