How to Declare an Array in C++? Last Updated : 18 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of the array and the size in square brackets. Syntax to Declare an Array in C++Datatype arrayName[arraySize];To directly initialize the elements in the array we can use the below syntax: Datatype ArrayName [ArraySize] = {element1, element2, ....}If we want to declare an array with more dimensions, we can just keep adding the dimensions after the first dimensions with their size inside the array subscript operator. C++ Program to Declare an ArrayThe below program illustrates how we can declare an array in C++. C++ // C++ Program to show how to declare an array #include <iostream> using namespace std; int main() { // Declaring an array of 4 elements int arr[4] = { 1, 2, 3, 4 }; // Print the elements of the array cout << "Array Elements: "; for (int i = 0; i < 4; i++) cout << arr[i] << " "; return 0; } OutputArray Elements: 1 2 3 4 Time Complexity: O(1), constant time required only for declaration.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Declare an Array in C++? A aayushi2402 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Empty an Array in C++? In C++, arrays are fixed-size containers that store elements of the same data type. Empty an array means removing all elements from it. In this article, we will see how to empty an array in C++. Example: Input: myArray = { 1, 8, 2, 9, 1, 5, 6 } Output: myArray = { 0, 0, 0, 0, 0, 0, 0 }Clear an Array 2 min read How to Declare a List in C++? In C++, list is a data structure used to store elements sequentially in non-contiguous memory locations. This container implements doubly linked list which contains pointers to both previous and next elements in the sequence. In this article, we will learn how to declare a list in C++. Declare a Lis 2 min read How to Declare a Vector in C++? In C++, the vector is a dynamic array that can resize itself automatically to accommodate new elements. In this article, we will learn how to declare a vector in C++. Declare a Vector in C++In C++, vectors are defined as the class templates in <vector> header file. So, to declare a std::vector 2 min read How to Create Array of Arrays in C++ Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra 3 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w 2 min read How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9}; 2 min read How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr 2 min read How to Check if an Array is Empty in C++? In C++, arrays are fixed-size data structures that allow the users to store similar data in contiguous memory locations. In this article, we will learn how to check if an array is empty in C++. Example: Input:arr[]={}arr[]={1,2,3,4,5}Output:The array is emptyThe array is not emptyCheck if an Array i 3 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read Like