C++ Data typesJanuary 28, 2021 2021-07-23 12:09
C++ Data types
C++ Data types – Introduction
In C++, the data type is defined as a type of the variable data that is declared with its variable name which is followed by such data type which means it will tell the user or developer that the variable declared in the program is of which type (information type that variable holds and the size) and types can be either int, float, double, etc data types for the declared variables. In C++, data types are mainly classified into 3 different groups such as the data types can be either derived data types, built-in data types, or user-defined data types in C++. But some of the common data type in C++ is classified into 5 types such as void, int, float, double, and char.
Classification of C++ Data types:
In C++, the data types are mainly classified into 3 types and they are as follows:
Derived data types:
In C++, the derived data types are defined as data types that are derived from the basic or built-in data types are known as derived data types. The derived data types are divided into 3 types and they are:
- Pointers: This type is defined as a type of any variable that holds the address of the variable declared as a pointer type. These pointers are mainly used for creating dynamic data structures and also can invoke call-by-reference in the program.
- Function: This type is defined as a type for declaring the function which is a set of codes written inside the body of the function with having proper logic for the set of codes that can be used in the program anywhere by just calling it once and can call any number of times with this function in the program.
- Array: This type of data type for declaring array types and the array is defined as a set of elements or items that are grouped and stored in any memory location and this location is known as an array. This type can be used to define one variable that can store the set of values to that variable by declaring this variable as an array.
Built-in data types:
In C++, Built-in data types are also referred to as basic type we should also note that the size of basic data types can vary from 32-bit to 64-bit OS and they are many different basic types and they are:
- Integer: This type is declared using the keyword “int” to any variable that needs to store the numeric values that can hold values up to 4 bytes but not decimal values.
- Float: This type store the decimal values or fractional numbers with the keyword “float” and its size is 4 bytes that are it can store up to 7 decimal digits.
- Double: It uses the keyword “double” and its size is 8 bytes that can store decimal numbers up to 15 decimal digits.
- Char: It uses the keyword “char” and its size is 1 byte that can store single letter or alphabet or character values.
User-defined data types:
These data types are the types of data where a user itself can define and declare it to the variable and they are classified as follows:
- Structure: This type is used with the keyword “struct” to create such types of data to store the set of elements that are of different types into one single type.
- Class: This type is used with the keyword “class” which can create an instance of this class and also this class can hold member functions and its data members which makes it look like a blueprint for any object that is created.
- Union: This uses the keyword “union” which is similar to as structure data type but all the members here are stored in the same memory location which means they share the same locations.
- Enumeration: This uses the keyword “enum” which is used for defining or giving a name to any constants that are integer type that helps in any program reading.
Primitive data types in C++:
In C++, primitive data types are defined as the values of the given variable that can represent the basic data type which is very important in basic program building which is the basic foundation data types for the complex types also. Some of them are:
- Integer types: These types stores numerical values but not decimal numbers.
- Floating and double points: These types can store numerical values that have decimal points or any fractional numbers with different sizes according to the OS.
- Character types: These types can store only a single letter or alphabet as a value.
- Boolean types: These types can store variables that can hold only true or false values or 0 or 1 as values.
- Void types: These types are said to represent any variable with no type.
Fundamental Data Types in C++
In C++, the fundamental data types are classified as follows:
- Void: This typically means it is an empty type of any variable declared with the keyword “void”.
- Boolean: This type indicates the variable that can store only two values which can be true or false, or 1 or 0, etc with “bool” as a keyword to the variable.
- Floating point: This type is in turn divided into the float, double, and long double data types that can store decimal values with different size of each type and can store with keyword “float”, “double” and “long double”.
- Integer: This type can store only numeric values up to only 4 bytes of size with “int” as a keyword.
- Character: This type can store only character type or letter or alphabets that can store up to 1 byte with “char” as a keyword.
- Wide characters: This is also a character type with a size up to 2 to 4 bytes long and represented as “wchar_t”.
C++ Type modifiers:
In C++, data type modifiers are defined as to modify the length of the data types that any variable can hold. They are:
- Signed: These are defined as the value stored can be of positive or negative values of any integer, char, or long types.
- Unsigned: These are defined to store a value that can be of only positive values for integer, short or char types.
- Long: These are defined to store integers that are long up to 4 bytes of integer or double types.
- Short: These are defined to store integers that are short up to 2 bytes of integers only.
C++ modified data type list:
- Signed int: Its size is 4 bytes used for both positive and negative integers ( similar to int)
- Unsigned int: Its size is also 4 bytes for storing only positive integer values.
- Long: This in turn as different lists of modifiers such as:
- Long: with size 4 bytes
- Unsigned long: with size 4 bytes and only positive values.
- Long long: with size 8 bytes
- Unsigned long: with size 8 bytes and only positive values.
- Long double: with size 12 bytes.
- Short: Its size is up to 2 bytes which are mainly for small integer types.
- Signed char: Used to store character with size up to 1 byte with range -127 to 127.
- Unsigned char: Used to store character of size 1 byte with range 0 to 255 which means only positive number range of characters.