# What Is Primitive Data Structure and Non-Primitive Data Structure?

//

Angela Bailey

What Is Primitive Data Structure and Non-Primitive Data Structure?

In computer science, data structures are used to organize and store data in a computer’s memory. These data structures can be broadly classified into two categories: primitive data structures and non-primitive data structures.

## Primitive Data Structures

A primitive data structure is a basic or fundamental type of data that is built into a programming language. These data types are predefined and have a fixed size. They are used to represent simple values and include:

• Integer: Used to store whole numbers. Examples include 0, 1, -5, etc.
• Float: Used to store decimal numbers. Examples include 3.14, -2.5, etc.
• Boolean: Used to represent logical values (true or false).
• Character: Used to store individual characters such as ‘a’, ‘B’, ‘@’, etc.

Note: Some programming languages may have additional primitive data types like byte, short, long, double, etc., but the ones mentioned above are commonly found in most languages.

## Non-Primitive Data Structures

A non-primitive data structure is a composite or derived type of data that is built using primitive data types or other non-primitive data structures. Unlike primitive types, they can be of variable size and have no predefined limits on their values. Non-primitive data structures include:

• Array: A collection of elements of the same type stored in contiguous memory locations.
• String: A sequence of characters. It is often implemented as an array of characters.
• Structure: A user-defined data type that can hold multiple values with different data types.
• Class: Similar to a structure, but it can also contain member functions (methods).

### Example: Array

An array is a non-primitive data structure that allows you to store multiple elements of the same type in a contiguous memory block. It provides random access to its elements using an index. Here’s an example of how to declare and use an array in C++:

``````
#include <iostream>
using namespace std;

int main() {
int numbers[5]; // Declaration of an integer array with size 5

// Assigning values to the array elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

// Accessing and printing array elements
for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << numbers[i] << endl;
}

return 0;
}
```
```

Note: The above code declares an integer array named "numbers" with a size of five. It assigns values to the individual elements and then prints them using a for loop.

## Conclusion

In summary, primitive data structures are basic data types built into programming languages, while non-primitive data structures are composite types derived from primitive types or other non-primitive structures. Understanding these concepts is essential for effective data organization and manipulation within programs.

By mastering these concepts, you'll be equipped to make informed decisions about which data structure to use in different scenarios, leading to more efficient and optimized code.