Index
Data Types
A data type defines the kind of value a variable can hold and the operations that can be performed on it. It essentially determines the size and range of values the variable can store, along with the set of possible operations applicable to that data.
There are three main categories of data types in C++
1.Primitive Data Types:
These are the fundamental building blocks and are predefined by the language. Common examples include:
The data type specifies the size and type of information the variable will store:
Data Type | Size | Description |
---|---|---|
boolean | 1 byte | Stores true or false values |
char | 1 byte | Stores a single character/letter/number, or ASCII values |
int | 2 or 4 bytes | Stores whole numbers, without decimals |
float | 4 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits |
double | 8 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits |
2.Derived Data Types:
These are built on top of primitive data types and provide more complex data structures. Examples include:
2.1 Arrays: Collections of elements of the same data type.
2.2 Pointers: Variables that store memory addresses of other variables.
2.3 References: Aliases for existing variables.
3.User-Defined Data Types:
These are created by programmers to suit specific needs. They allow you to group related data and functions under a single unit. Examples include:
3.1 Structures: User-defined collections of variables of different data types.
3.2 Classes: Encapsulate data (member variables) and functions (member functions) that operate on that data.
3.3 Enumerations (enums): User-defined types consisting of named integer constants.
Understanding data types is essential for effective C++ programming, as they influence memory allocation, code efficiency, and the range of values you can work with in your programs.