Data Types in CPP

Data Types in CPP

There are Mainly Two types of Data Types in Cpp

  1. Primitive Data Types

  2. User Defined Data Types

  3. Derived Data Types

What are Data Types in C++? Types

Primitive Data Types in C++

Users can use the primitive data types to declare variables, and these are built-in data types in C++, for instance, float, bool, etc. Primitive data types present in C++ are defined below:

1. Integer

The keyword int can represent integer data types. The range of integers is -2147483648 to 2147483647, and they take up 4 bytes of memory.

For example,

 int data = 1526;

data here is an integer data type variable. The variable data requires 2 or 4 bytes of memory space.

2. Character

The keyword char represent characters. It is 1 byte in size. Single quotes ' ' are used to enclose characters in C++.

For example,

 char ch = 's';

ch here is a character datatype variable. This means that the variable requires 1 byte of memory space.

3. Boolean

The boolean data type's keyword is bool. True or false are the two possible values for the boolean data type. Boolean values are generally used in conditional statements and loops.

For example,

 bool is_true = true;

is_true here is a boolean data type variable. This means that the variable requires 1 byte of memory space.

4. Floating Point

float is the keyword used to hold floating-point numbers (decimals and exponentials). The float variable has a size of 4 bytes.

For example,

 float val = 15.26;

val here is a floating-point datatype variable. This means that the variable requires 4 bytes of memory space.

5. Double Floating Point

Double is the keyword used to hold floating-point numbers (decimals and exponentials) with double precision. The double variable has a size of 8 bytes.

For example,

 double val = 2019.1526;

val here is a double floating-point datatype variable. This means that the variable requires 8 bytes of memory space.

User Defined Data Types

1. Class

A Class is a C++ building piece that leads to Object-Oriented programming. It's a user-defined data type with its own set of data members and member functions that can be accessed and used by establishing a class instance. A class defines the blueprint for a data type.

2. Structure

A structure datatype is a user-defined data type that combines objects of potentially different data types into a single type.

3. Union

Union is similar to Structures as it is also used to combine the different types of data into a single user-defined data type. All members of a union have access to the same memory. In the below-shown example, we can combine the integer data type and the character data type into a single data type called test. In this case, as both the data types, integer, and character have different data sizes, we would take the larger data type as the size of the new user-defined data type test.

4. Enumeration

In C++, an enumeration (or enum) is a data type that the user creates. It's primarily used to give integral constant names, making the program easier to comprehend and maintain. In enumeration, if we do not provide the integral values explicitly to the strings, then, in that case, the strings automatically start assigning the integral values starting from value 0.

Derived Data Types

1. Array

An array is a set of elements that are kept in memory in a continuous manner and also have the same type of data present within the array. The purpose of an array is to store a lot of data in a single variable name and sequential order.

2. Pointer

Pointers are symbolic representations of addresses. Pointers store the addresses of the variables having the same datatype as that of the pointer. The size of the pointer is either 4 bytes or 8 bytes, no matter what the data type is. They enable programs to create and change dynamic data structures, as well as to imitate call-by-reference. In C/C++, its generic declaration looks like this:

3. Reference

When we declare a variable as a reference, it becomes an alternate name for an existing variable. By adding '&' to a variable's declaration, it can be declared as a reference.

We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.