An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, etc.

Array Declaration
In C, we have to declare the array like any other variable before using it. We can declare an array by specifying its name, the type of its elements, and the size of its dimensions. When we declare an array in C, the compiler allocates the memory block of the specified size to the array name.
C
data_type array_name[size];
Create an array with the name array_name, and it can store a specified number of elements of the same data type.
Example:
C
The above statement will create an array arr that can store 5 integer values.

Array Declaration
Array Initialization
Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated memory, the elements of the array contain some garbage value. So, we need to initialize the array to some meaningful values by using initializer list is the list of values enclosed within braces { } separated by a comma.
C
int arr[5] = {2, 4, 8, 12, 16};
The above statement creates an array arr and assigns the values {2, 4, 8, 12, 16} at the time of declaration. We can skip some value, If we skip some values during initialization, it is called partial initialization.
C
We can also skip the size of the array.
C
int arr[] = {2, 4, 8, 12, 16};
If we want to initialize an array with all elements set to 0.
C

Array Initialization
Accessing
We can access any element of the array by providing the position of the element, called the index. Indexes start from 0. We pass the index inside square brackets [] with the name of the array.
C
where, index value lies into this range – (0 ≤ index ≤ size-1).
Example:
C
#include <stdio.h>
int main() {
// array declaration and initialization
int arr[5] = {2, 4, 8, 12, 16};
// accessing element at index 2 i.e 3rd element
printf("%d\n", arr[2]);
// accessing element at index 4 i.e last element
printf("%d\n", arr[4]);
// accessing element at index 0 i.e first element
printf("%d", arr[0]);
return 0;
}

Update Array Element
We can update the value of an element at the given index i in a similar way to accessing an element by using the array square brackets [] and assignment operator (=).
C
array_name[i] = new_value;
Example:
C
//Driver Code Starts{
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Update the first value
// of the array
//Driver Code Ends }
arr[0] = 1;
printf("%d", arr[0]);
return 0;
}
C Array Traversal
Traversal is the process in which we visit every element of the data structure. For C array traversal, we use loops to iterate through each element of the array.
C
for (int i = 0; i < N; i++) {
array_name[i];
}
Example:
C
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Print each element of
// array using loop
for(int i = 0; i < 5; i++){
printf("%d ", arr[i]);
}
return 0;
}

Size of Array
The size of the array refers to the number of elements that can be stored in the array. We can extract the size using sizeof() operator.
Example:
C
//Driver Code Starts{
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
//Driver Code Ends }
// Size of the array
int size = sizeof(arr)/sizeof(arr[0]);
printf("%d", size);
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
- The sizeof() operator returns the size in bytes. sizeof(arr) returns the total number of bytes of the array.
- In an array, each element is of type int, which is 4 bytes. Therefore, we can calculate the size of the array by dividing the total number of bytes by the byte size of one element.
Passing array to Function
In C, arrays are passed to functions using pointers, as the array name decays to a pointer to the first element. There are three common methods:
- Unsized array notation, where the array is passed as a pointer without specifying its size.
- Sized array notation, where the size is included in the declaration for clarity, but it’s still passed as a pointer.
- Pointer notation, where the array is explicitly passed as a pointer, often along with its size.
Example:
C
//Driver Code Starts{
#include <stdio.h>
//Driver Code Ends }
// Functions that take array as argument
void sized_array_notation(int arr[5]) {}
void unsized_array_notation(int arr[]) {}
void pointer_notation(int* arr) {}
int main() {
int arr[] = {2, 4, 8, 12, 16};
int n = sizeof(arr) / sizeof(arr[0]);
// Passing array
sized_array_notation(arr);
unsized_array_notation(arr);
pointer_notation(arr);
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Multidimensional Array in C
In the above, we only discussed one-dimension array. Multi-dimensional arrays in C are arrays that grow in multiple directions or dimensions. A one-dimensional array grows linearly, like parallel to the X-axis, while in a multi-dimensional array, such as a two-dimensional array, the elements can grow along both the X and Y axes.
C
return_type array_name[size1]
[size2] .. [sizen];
Two-Dimensional Array in C
A Two-Dimensional array or 2D array in C is an array that has exactly two dimensions. They can be visualized in the form of rows and columns organized in a two-dimensional plane.
Example:
C
//Driver Code Starts{
#include <stdio.h>
int main() {
//Driver Code Ends }
// 2D array declaration
int arr[4][4];
// Initialize 2d array
for(int i = 0; i<4; i++){
int val = 1;
for(int j = 0; j < 4; j++){
arr[i][j] = val++;
}
}
//Driver Code Starts{
// Print 2d array
for(int i = 0; i<4; i++){
for(int j = 0; j < 4; j++){
printf("%d ", arr[i][j]);
}
printf("
");
}
return 0;
}
//Driver Code Ends }
Output1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Three-Dimensional Array in C
Another popular form of a multi-dimensional array is Three-Dimensional Array or 3D Array. A 3D array has exactly three dimensions. It can be visualized as a collection of 2D arrays stacked on top of each other to create the third dimension.
Example:
C
//Driver Code Starts{
#include <stdio.h>
int main() {
//Driver Code Ends }
// 3D array declaration (3 layers, 3 rows, 3 columns)
int arr[3][3][3];
// Filling the 3D array
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
int val = 1;
for(int k = 0; k < 3; k++) {
arr[i][j][k] = val++;
}
}
}
//Driver Code Starts{
// Printing the 3D array
for(int i = 0; i < 3; i++) {
printf("Layer %d:
", i+1);
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++) {
printf("%d ", arr[i][j][k]);
}
printf("
");
}
printf("
");
}
return 0;
}
//Driver Code Ends }
OutputLayer 1:
1 2 3
1 2 3
1 2 3
Layer 2:
1 2 3
1 2 3
1 2 3
Layer 3:
1 2 3
1 2 3
1 2 3

To know more about Multidimensional Array in C, refer to this article – Multidimensional Arrays in C
Relationship between Arrays and Pointers
Arrays and Pointers are closely related to each other such that we can use pointers to perform all the possible operations of the array. The array name is a constant pointer to the first element of the array and the array decays to the pointers when passed to the function.
C
//Driver Code Starts{
#include <stdio.h>
int main() {
//Driver Code Ends }
int arr[5] = { 10, 20, 30, 40, 50 };
int* ptr = &arr[0];
// Address store inside
// name
printf("%p
", arr);
// Print the address which
// is pointed by pointer ptr
printf("%p
", ptr);
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output0x7ffde73e54b0
0x7ffde73e54b0
To know more about the relationship between an array and a pointer, refer to this article – Pointer to an Arrays | Array Pointer.
Properties of Arrays in C
It is very important to understand the properties of the C array so that we can avoid bugs while using it. The following are the main properties of an array in C:
- The array in C is a fixed-size collection of elements. The size of the array must be known at the compile time, and it cannot be changed once it is declared.
- We can only store one type of element in an array.
- The array index always starts with 0 in C language. It means that the index of the first element of the array will be 0 and the last element will be N – 1.
- All the elements in the array are stored continuously one after another in the memory.
- The array in C provides random access to its element i.e we can get to a random element at any index of the array in constant time complexity just by using its index number.
- There is no index out-of-bounds checking in C.
Advantages of Array in C
The following are the main advantages of an array:
- Random and fast access of elements using the array index.
- Use of fewer lines of code as it creates a single array of multiple elements.
- Traversal through the array becomes easy using a single loop.
- Sorting becomes easy as it can be accomplished by writing fewer lines of code.
Disadvantages of Array in C
- Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
- Insertion and deletion of elements can be costly since the elements are needed to be rearranged after insertion and deletion.
Similar Reads
C Arrays
An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, etc. Array DeclarationIn C,
9 min read
Properties of Array in C
An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
8 min read
Length of Array in C
The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements. In C language, we don't have any pre-defi
3 min read
Multidimensional Arrays in C - 2D and 3D Arrays
Prerequisite: Arrays in C A multi-dimensional array can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays are 2D arrays and 3D arrays. In this article, we will learn about multid
10 min read
Initialization of Multidimensional Array in C
In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial
4 min read
Jagged Array or Array of Arrays in C with Examples
Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example: arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}
3 min read
Pass Array to Functions in C
Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C. In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, wh
3 min read
How to pass a 2D array as a parameter in C?
A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function. The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and c
3 min read
How to pass an array by value in C ?
In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
2 min read
Variable Length Arrays (VLAs) in C
In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time. Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope o
2 min read