While writing a program, we store the information by creating a variable. These variables are nothing but the name given to some unspecified memory location. Now the information can be character type, integer type, double, float etc.
A data type specifies the type of data that a variable can store such as integer, floating, character, double, boolean etc. Each of these data types varies in storage size that will be shown below in the table.
A data type or simply type is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data.
Types of data types in C++:
- Primitive Data Types: integer, float, character, double, boolean, void and wchar_t.
- Derived Data Types: Arrays, Pointers and Function.
- Abstract or User-Defined Data Types: Structure, Union and Enum.
Primitive Data Types:
These are the predefined data types that are already defined in C++ such as integer, float, character, double, boolean, void and wchar_t.
Basic Built in Datatypes in C++:
The memory size of data types may change according to 32 or 64 bit Operating System (OS).
data type | desc and size |
---|---|
char | for character storage (1 byte) |
int | for integral number (2 bytes) |
float | single precision floating point (4 bytes) |
double | double precision floating point numbers (8 bytes) |
bool | Boolean (True or False) |
void | Without any Value |
wchar_t | Wide Character. (2 or 4 bytes) |
**Wide Character(wchar_t
): This type should be avoided because its size is implementation-defined and not reliable.
How above basic data type is written in code, example:
1 2 3 4 5 | char ch = 'A'; // character type int num = 10; // integer type float num = 627.5485; // floating point type double num = 20054.86798; //double type (e is for exponential) bool b = true; //boolean type returns true or false |
Modifiers in C++:
The modifiers for the built-in data is used specify them more precisely and even increase the range. Following modifiers are used with the above basic data types:
- signed: includes both positive and negative numbers.
- unsigned: refers to the numbers that are always without any sign, that are always positive.
- short: minimum values that a data type will hold.
- long: maximum values that a data type will hold.
Size hierarchy: short int < int < long int
Size hierarchy for floating point numbers is: float < double < long double
Data Types (s & u) | Memory Size | Range |
---|---|---|
char | 1 byte | -128 to 127 |
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 127 |
short | 2 byte | -32,768 to 32,767 |
signed short | 2 byte | -32,768 to 32,767 |
unsigned short | 2 byte | 0 to 32,767 |
int | 2 byte | -32,768 to 32,767 |
signed int | 2 byte | -32,768 to 32,767 |
unsigned int | 2 byte | 0 to 32,767 |
short int | 2 byte | -32,768 to 32,767 |
signed short int | 2 byte | -32,768 to 32,767 |
unsigned short int | 2 byte | 0 to 32,767 |
long int | 4 byte | |
signed long int | 4 byte | |
unsigned long int | 4 byte | |
float | 4 byte | |
double | 8 byte | |
long double | 10 byte |
Derived Data Types:
Also known as Non-Primitive data type. These are:
- Arrays
- Pointers
- Function.
These topics are covered in detail in separate tutorials.
Abstract or User-Defined Data Types:
These are the type, that user creates as a class or a structure. There three types of user-defined data types in C++:
- struct
- union
- enum
These topics are covered in detail in separate tutorials.