Easy Tutorial
❮ C Function Gmtime Function Call Operator Overloading ❯

C++ Data Types

When programming with a programming language, various variables are needed to store different types of information. Variables hold the memory location of the values they store. This means that when you create a variable, some space is reserved in memory.

You may need to store information of various data types (such as character, wide character, integer, floating point, double floating point, boolean, etc.). The operating system allocates memory based on the variable's data type and decides what to store in the reserved memory.

Basic Built-in Types

C++ provides programmers with a wide variety of built-in and user-defined data types. The following table lists seven basic C++ data types:

Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Void void
Wide character wchar_t

In fact, wchar_t is derived as follows:

typedef short int wchar_t;

So the space actually occupied by wchar_t is the same as that of short int.

Some basic types can be modified using one or more type modifiers:

The following table shows the memory required to store values for various variable types and the maximum and minimum values that the type can store.

Note: Differences may exist across systems, with one byte being 8 bits.

Note: By default, int, short, and long are signed, i.e., signed.

Note: long int is 8 bytes, and int is 4 bytes. Early C compilers defined long int as occupying 4 bytes and int as 2 bytes. Newer versions of C/C++ standard have aligned with this earlier specification.

Type Bits Range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0 to 4294967295
signed int 4 bytes -2147483648 to 2147483647
short int 2 bytes -32768 to 32767
unsigned short int 2 bytes 0 to 65,535
signed short int 2 bytes -32768 to 32767
long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int 8 bytes 0 to 18,446,744,073,709,551,615
float 4 bytes Precision type occupies 4 bytes (32 bits) of memory space, +/- 3.4e +/- 38 (~7 digits)
double 8 bytes Double precision type occupies 8 bytes (64 bits) of memory space, +/- 1.7e +/- 308 (~15 digits)
long double 16 bytes Long double precision type occupies 16 bytes (128 bits) of memory space, providing 18-19 significant digits.
wchar_t 2 or 4 bytes 1 wide character

>

Note that the storage size of various types is related to the system's bit count, but currently, the dominant system is 64-bit.

The following lists the differences in storage size between 32-bit and 64-bit systems (same for Windows):

From the table above, it can be seen that the size of a variable may vary depending on the compiler and the computer used.

The following example will output the size of various data types on your computer.

Example

#include<iostream>  
#include <limits>

using namespace std;  

int main()  
{  
    cout << "type: \t\t" << "************size**************"<< endl;  
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);  
    cout << "\t最大值:" << (numeric_limits<bool>::max)();  
cout << "\t\tMinimum value: " << (numeric_limits<bool>::min)() << endl;  
cout << "char: \t\t" << "Size in bytes: " << sizeof(char);  
cout << "\tMaximum value: " << (numeric_limits<char>::max)();  
cout << "\t\tMinimum value: " << (numeric_limits<char>::min)() << endl;  
cout << "signed char: \t" << "Size in bytes: " << sizeof(signed char);  
cout << "\tMaximum value: " << (numeric_limits&lt;signed char>::max)();  
cout << "\t\tMinimum value: " << (numeric_limits&lt;signed char>::min)() << endl;  
cout << "unsigned char: \t" << "Size in bytes: " << sizeof(unsigned char);  
cout << "\tMaximum value: " << (numeric_limits&lt;unsigned char>::max)();  
cout << "\t\tMinimum value: " << (numeric_limits&lt;unsigned char>::min)() << endl;  
cout << "wchar_t: \t" << "Size in bytes: " << sizeof(wchar_t);  
cout << "\tMaximum value: " << (numeric_limits<wchar_t>::max)();  
cout << "\t\tMinimum value: " << (numeric_limits<wchar_t>::min)() << endl;  
cout << "short: \t\t" << "Size in bytes: " << sizeof(short);  
cout << "\tMaximum value: " << (numeric_limits<short>::max)();  
cout << "\t\tMinimum value: " << (numeric_limits<short>::min)() << endl;  
cout << "int: \t\t" << "Size in bytes: " << sizeof(int);  
cout << "\tMaximum value: " << (numeric_limits<int>::max)();  
cout << "\tMinimum value: " << (numeric_limits<int>::min)() << endl;  
cout << "unsigned: \t" << "Size in bytes: " << sizeof(unsigned);  
cout << "\tMaximum value: " << (numeric_limits<unsigned>::max)();  
cout << "\tMinimum value: " << (numeric_limits<unsigned>::min)() << endl;  
cout << "long: \t\t" << "Size in bytes: " << sizeof(long);  
cout << "\tMaximum value: " << (numeric_limits<long>::max)();  
cout << "\tMinimum value: " << (numeric_limits<long>::min)() << endl;  
cout << "unsigned long: \t" << "Size in bytes: " << sizeof(unsigned long);  
cout << "\tMaximum value: " << (numeric_limits&lt;unsigned long>::max)();  
cout << "\tMinimum value: " << (numeric_limits&lt;unsigned long>::min)() << endl;  
cout << "double: \t" << "Size in bytes: " << sizeof(double);  
cout << "\tMaximum value: " << (numeric_limits<double>::max)();  
cout << "\tMinimum value: " << (numeric_limits<double>::min)() << endl;  
cout << "long double: \t" << "Size in bytes: " << sizeof(long double);  
cout << "\tMaximum value: " << (numeric_limits&lt;long double>::max)();  
cout << "\tMinimum value: " << (numeric_limits&lt;long double>::min)() << endl;  
cout << "float: \t\t" << "Size in bytes: " << sizeof(float);  
cout << "\tMaximum value: " << (numeric_limits<float>::max)();  
cout << "\tMinimum value: " << (numeric_limits<float>::min)() << endl;  
cout << "size_t: \t" << "Size in bytes: " << sizeof(size_t);  
cout << "\tMaximum value: " << (numeric_limits<size_t>::max)();  
cout << "\tMinimum value: " << (numeric_limits<size_t>::min)() << endl;  
cout << "string: \t" << "Size in bytes: " << sizeof(string) << endl;  
// << "\tMaximum value: " << (numeric_limits<string>::max)() << "\tMinimum value: " << (numeric_limits<string>::min)() << endl;  
cout << "type: \t\t" << "************size**************"<< endl;  
return 0;  
}

This example uses endl, which inserts a newline character after every line, and the << operator is used to send multiple values to the screen. The sizeof() operator is used to obtain the size of various data types.

When the above code is compiled and executed, it produces the following result, which may vary depending on the computer used:

type:         ************size**************
bool:         Size in bytes: 1     Maximum value: 1      Minimum value: 0
char:         Size in bytes: 1     Maximum value:       Minimum value: ?
signed char:      Size in bytes: 1     Maximum value:       Minimum value: ?
unsigned char:      Size in bytes: 1     Maximum value: ?      Minimum value: 
wchar_t:      Size in bytes: 4     Maximum value: 2147483647      Minimum value: -2147483648
short:         Size in bytes: 2     Maximum value: 32767      Minimum value: -32768
int:         Size in bytes: 4     Maximum value: 2147483647      Minimum value: -2147483648
unsigned:      Size in bytes: 4     Maximum value: 4294967295      Minimum value: 0
long:         Size in bytes: 8     Maximum value: 9223372036854775807      Minimum value: -9223372036854775808
unsigned long:      Size in bytes: 8     Maximum value: 18446744073709551615      Minimum value: 0
double:      Size in bytes: 8     Maximum value: 1.79769e+308      Minimum value: 2.22507e-308
long double:      Size in bytes: 16     Maximum value: 1.18973e+4932      Minimum value: 3.3621e-4932
float:         Size in bytes: 4     Maximum value: 3.40282e+38      Minimum value: 1.17549e-38
size_t:      Size in bytes: 8     Maximum value: 18446744073709551615      Minimum value: 0
string:      Size in bytes: 24
type:         ************size**************

typedef Declaration

You can use typedef to give a new name to an existing type. The syntax for defining a new type using typedef is as follows:

typedef type newname;

For example, the following statement tells the compiler that feet is another name for int:

typedef int feet;

This is a Chinese to English translation. Here is the English translation for the text:

int distance;

Enumeration Types

An enumeration type in C++ is a derived data type that is defined by a user as a collection of enumerated constants.

If a variable has only a few possible values, it can be defined as an enumeration type. An "enumeration" is the act of listing the variable's values one by one, with the variable's values restricted to those listed.

To create an enumeration, the keyword enum is used. The general form of an enumeration type is:

enum EnumName { 
     Identifier[=integer constant], 
     Identifier[=integer constant], 
     ... 
     Identifier[=integer constant]
} EnumVariable;

If the enumeration is not initialized, i.e., omitting "=integer constant", it starts from the first identifier.

For example, the following code defines a color enumeration, with variable c of type color. Finally, c is assigned the value "blue".

enum color { red, green, blue } c;
c = blue;

By default, the first name has a value of 0, the second name has a value of 1, the third name has a value of 2, and so on. However, you can also assign a specific value to a name by adding an initial value. For example, in the following enumeration, green is assigned a value of 5.

enum color { red, green=5, blue };

Here, blue has a value of 6, because by default, each name is one greater than the previous one, but red still has a value of 0.

❮ C Function Gmtime Function Call Operator Overloading ❯