Data Types in C++
C++ Data Types
Data Types are used to define the operations and store each of them. It can be classified into three major categories:
1. User-defined data type
2. Built-in data type
3. Derived data type
User-defined data type
In C++, User-defined data types are Structure, Union, Class, enum etc.
- Structure: Structures are used for grouping elements with dissimilar types. It is often required to group logically related data items. It has the following syntax:
struct name { data type member1; data type member2; .............. }
- Union: Unions are conceptually similar to structures, it allows us to group dissimilar type elements inside a single unit. In unions, the same memory space is used for representing different member elements. As a result, union members can only be manipulated exclusively by each other. It has the following syntax:
union name { data type member1; data type member2; .............. }
- Class: In C++, Class is a user-defined data type which is used to declare variables.
- Enum: An enumerated data type is also a user-defined data type which provides a way for attaching names to numbers, increasing the comprehensibility of the code. The enum keyword is used to assign those enumerate values.
enum shape{square, cirle}; enum color{blue, green};
Built-in data type:
In C++, Built-in data types are given as follows:
Type | Bytes | Range |
char | 1 | -128 to 127 |
unsigned | 1 | 0 to 255 |
int | 2 | -32768 to 32767 |
unsigned int | 2 | 0 to 65535 |
long int | 4 | -2147483648 to 2147483647 |
unsigned long int | 4 | 0 to 4294967295 |
float | 4 | 3.4E-38 to 3.4E+38 |
double | 8 | 1.7E-308 to 1.7E+308 |
long double | 10 | 3.4E-4932 to 1.1E+4932 |