Primitive and Non-Primitive Datatypes in C++

Hello there! In this post, we will discuss primitive and Non-primitive Datatypes in C++. So, let’s start without wasting time!

Introduction

What are Primitive Datatypes?

In simple terms “data type” and “primitive data type” are simply known and used as interchangeable variables. Primitive data types are predefined types of data, which are supported by any programming language. For example- integer, character, and string, are all primitive data types.

What are Non-primitive Datatypes?

Non-primitive data types are not defined by the programming language but are instead created by the programmer i.e us. They are sometimes called “reference variables,” or “object references,” since they act as a reference to a memory location, which stores the data.

Classification of Datatypes

In the above image, we can observe the classification of the data structure. In the case of primitive data structure, it contains fundamental data types such as integer, float, character, pointer.

These fundamental data types can hold a single type of value.

For example, an integer variable can hold an integer type of value. The float variable can hold a floating type of value. Character variables can hold a character type of value. Whereas, the pointer variable can hold the pointer type of value.

Primitive Datatype

A primitive data type is one that fits the base architecture of the underlying computer such as int, float, and pointer, and all of the variations, thereof such as char, short, long, unsigned, float, double and etc, are primitive data types.

Primitive data are only single values, they have no special capabilities. Let us discuss few primitive datatypes in this post.

Boolean data type

A boolean data type comprises a bit of information and can store only true or false values. This data type is used to track true/false conditions.

Example-

#include <iostream>
using namespace std;

int main()
{
    bool var1 = true;
    bool var2 = false;
    //printing the values
    cout << "var1 : " << var1 << endl;
    cout << "var2 : " << var2 << endl;
   
    return 0;
}

Output

var1 : 1
var2 : 0

int data type

This data type can store whole numbers from -2147483648 to 2147483647. Mainly, it is the preferred data type when you create variables with a numeric value.

int myNum = 1000;
cout << myNum;

float data type

Float is a shortened term for “floating-point.” By definition, it’s a fundamental data type built into the compiler that’s used to define numeric values with floating decimal points. C, C++, C#, and many other programming languages recognize float as a data type.

float myNum = 5.75;
cout << myNum;

double data type

The double is a fundamental data type built into the compiler and used to define numeric variables holding numbers with decimal points. It can contain up to 15 digits in total, including those before and after the decimal point.

double myNum = 19.99;
cout << myNum;

byte data type

This is an example of a primitive data type. It is an 8-bit signed two’s complement integer. It stores whole numbers that lie between -128 to 127. A byte data type is helpful for saving memory in large amounts. 

char data type

This data type is used to store a single character. The character must be enclosed within single quotes, like ‘E’ or ‘e’. Alternatively, you can also use ASCII values to display certain characters.

short data type

A short data type is greater than a byte in terms of size and less than an integer. It stores the value that ranges from  -32,768 to 32767. The default size of this data type: 2 bytes.

long data type

This data type is a 64-bit two’s complement integer. By default, the size of a long data type is 64 bit and its value ranges from -263 to 263-1.

Non-Primitive Datatype

A non-primitive data type is something as an array structure or class is known as the non-primitive data type.

It is also the data type that is derived from the primary data type. Examples of the non-primitive data types are Array, structure, union, link list, stacks, queue, etc…

Let’s now understand these non-primitive data types in short.

Strings

The string is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. 

Arrays

Arrays in Java are homogeneous data structures implemented in Java as objects. They store one or more values of a specific data type and provide indexed access to store the same. An element in an array is accessed by its index. 

Classes

A class is a blueprint that includes all your data.  A class contains fields(variables) and methods to describe the behavior of an object.

Interface

Like a class, an interface can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, no body).

So, that was all about the non-primitive data types. Now let’s understand the difference between primitive and non-primitive data types.

Conclusion

In this post, we have successfully studied the primitive and non-primitive data types in C++.

  1. Algorithm for graph coloring
  2. Modular arithmetics explained
  3. Sliding window technique explained
  4. Input output in java

Leave a Comment

Your email address will not be published. Required fields are marked *