C++ Data Types

When programming with a programming language, various variables are needed to store different types of information.
A variable retains the memory location of the value it stores. This means that when a variable is created, some space is reserved in memory.Various data types (such as character, wide character, integer, floating-point, double floating-point, boolean, etc.) may need to be stored, and the operating system allocates memory and determines what to store in the reserved memory based on the variable’s data type.
01
Basic Built-in Types
C++ provides programmers with a rich variety of built-in data types and user-defined data types.
The table below lists seven basic C++ data types:
C++ Data Types
Some basic types can be modified with one or more type modifiers:
signedunsignedshortlong
The table below shows the memory required to store values of various variable types, as well as the maximum and minimum values that can be stored by that type of variable.
C++ Data Types
Note: Different systems may vary, one byte is 8 bits.
Note: By default, int, short, and long are all signed.
Note: long int is 8 bytes, int is 4 bytes; early C compilers defined long int as 4 bytes and int as 2 bytes. The new C/C++ standards are compatible with this early definition.
The storage size of various types is related to the system architecture, but currently, 64-bit systems are dominant.
C++ Data Types
As shown in the figure above,
the difference in storage size between 32-bit and 64-bit systems (same for Windows);
The size of a variable may vary depending on the compiler and the computer used.
02
typedef Declaration
Using typedef, you can give a new name to an existing type.
Example: Syntax to define a new type
typedef type newname;
Example: Tell the compiler that feet is another name for int:
typedef int feet;
Example: Create an integer variable distance:
feet distance;
03
Enumeration Type
An enumeration type is a derived data type in C++ that is a collection of user-defined enumeration constants.
If a variable has only a few possible values, it can be defined as an enumeration type.
Enumeration means listing the values of the variable one by one, and the value of the variable can only be within the enumerated range.
To create an enumeration, you need to use the keyword enum:
enum EnumName {      Identifier[=IntegerConstant],      Identifier[=IntegerConstant], ...     Identifier[=IntegerConstant]} EnumVariable;
If the enumeration is not initialized, meaning the “=IntegerConstant” is omitted, it starts from the first identifier.
For example, the code below defines a color enumeration, and the variable c is of type color. Finally, c is assigned the value “blue”.
enum color { red, green, blue } c;c = blue;
By default, the value of the first name is 0, the second name is 1, the third name is 2, and so on.
However, you can also assign a special value to a name by adding an initial value.
For example, in the following enumeration, green has the value of 5.
enum color { red, green=5, blue };
Here, the value of blue is 6 because, by default, each name is 1 greater than the previous name, but the value of red is still 0.
——- END ——-
Disclaimer

This article includes some illustrations and text sourced from the internet.

If there are any copyright issues, please contact us in a timely manner.

Leave a Comment