A data type in C++ describes the type of data that a variable can hold. The format and size of values that can be stored in a variable are defined.
There are 3 different Data types in C++, which are:
In C++, primitive data types are built-in data types that can be used directly by the user when declaring variables. Some of the primitive data types in C++ are:
Integer data types describe whole numbers that do not contain any fractions or decimal parts. They may be signed (positive, negative, or zero) or unsigned (only positive or zero).
Character data types represent specific characters from a character set, such as ASCII or Unicode. In C++, the term 'char' is widely used to represent characters.
Boolean data types represent binary values, which are commonly used for true (1) or false (0) conditions. In C++, 'bool' is used to represent Boolean data.
Floating-point data types represent numbers that contain a fractional portion. In C++, 'float' refers to a single-precision floating-point type.
Double-precision floating-point data types are used to represent numbers with a wider range and more precision than 'float'. In C++, the term 'double' is often used.
In C++, the void data type is used to indicate that a function returns no value or to specify generic pointers that do not refer to a specific data type.
Wide character data types, such as 'wchar_t', are used to represent characters from expanded character sets, such as Unicode characters, which take up more storage than a conventional 'char'.
In C++, the primitive data type gives rise to the derived data type. There are some derived data types in the C++ language.
A function is a reusable part of code that completes a certain task. It is a derived data type because it allows you to construct functions that interact with other data types and can even return results of multiple data types.
An array is a derived data type that represents a collection of identical data pieces stored in contiguous memory regions. The elements can be accessed using their indexes.
A pointer is a derived data type that contains the memory address of another data type. Pointers are commonly used for dynamic memory allocation as well as direct memory access.
A reference is a derived data type that serves as an alias or alternate name for an existing variable. It allows you to directly manipulate the original variable, which cannot be null or reseated to another variable after initialization.
Users define abstract data types in the C++ programming language. It is like to define a class in structure or C++. This data type has a few variations, which are:
A class is a user-defined data type that provides a blueprint for generating things. It encapsulates both data (attributes) and the functions (methods) that work with it. Classes are used to represent real-world entities and to construct bespoke data structures.
A structure is a user-defined data type that combines variables from many data types under a single name. Structures are useful for grouping related data components.
A union is a user-defined data type that enables you to store many data kinds in the same memory region. Unlike structures, which allot memory to all members, unions distribute memory among their members.
An enumeration is a user-defined data type made up of a collection of named integer constants. It allows you to define symbolic names for values, which improves code readability.
typedef is a keyword that allows you to define aliases for existing data types, improving code readability and abstraction. It is not a new data type, but rather a method of creating different names for existing ones.
In C++, data type modifiers are used to change the behavior and storage properties of basic data types. C++ has four modifiers. These modifiers can be used to modify data types such as integers, doubles, and characters. They are as follows:
The signed modifier indicates that a variable can have both positive and negative values. However, most built-in integer types (such as int) are signed by default in C++. As a result, using the signed keyword directly is rarely necessary.
The unsigned modifier indicates that a variable can only have non-negative values (zero or positive). It is commonly used when you know that a variable should not contain negative values and want to save memory by avoiding storing negative numbers.
The short modifier indicates that a variable should be stored with fewer bits than the normal data type. This is commonly used to save memory when you know a variable's value will fall inside a narrower range.
The long modifier indicates that a variable should be stored with more bits than the usual data type. This is used for storing larger values that do not fit within the range of a standard data type.