Last Updated : 26 Jul, 2025
In C, a 3D array is a type of multidimensional array that stores data in a three-dimensional grid. It has three dimensions, allowing it to store data in three directions: rows, columns, and depth. In this article, we will learn how to initialize a 3D array in C.
There are three ways in which a 3D array can be initialized in C:
Initialization Using Initializer ListWe can initialize a 3D array at the time of declaration by providing the values in curly braces {}
using the following syntax:
int arr[2][3][2] = { { {0, 1}, {2, 3}, {4, 5} },
{ {6, 7}, {8, 9}, {10, 11} }};
Here, the braces will help in navigating into the layers of the 3D array. The outermost set of braces groups all the elements of the 3D array. The second set of braces defines the specific 2D array (or depth level) starting from index 0. The innermost set of braces holds the values for each row within that 2D array, assigning values from row 0 to the last row.
We can also skip the inner braces and initialize the arrays as:
C Program to Initialize a 3D Array using Initializer List Cint arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
// C Program to illustrate how to initialize 3D array
// using initializer list
#include <stdio.h>
int main() {
// Directly initializing 3D array at the time of
// declaration
int arr[2][3][2] = { { {0, 1}, {2, 3}, {4, 5} },
{ {6, 7}, {8, 9}, {10, 11} }};
// Printing the array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
0 1 2 3 4 5 6 7 8 9 10 11
Time Complexity: O(d * m * n), where d is the number of 2D arrays (or depth), m and n are the number of rows and columns in each 2D array.
Auxiliary Space: O(1)
C language also provides a feature to initialize all the elements of the 3D array to 0. This feature is limited to the numeric arrays or other type that can be converted to numeric values.
C Program to Initialize 3D Array with Zero Cint arr[2][3][2] = { 0 }
// C Program to illustrate how to initialize
// a 3D array with zero
#include <stdio.h>
int main() {
// Initializing a 2x3x2 3D array
int arr[2][3][2] = {0};
// Printing the 3D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
0 0 0 0 0 0 0 0 0 0 0 0
Time Complexity: O(d * m * n), where d is the number of 2D arrays (or depth), m and n are the number of rows and columns in each 2D array.
Auxiliary Space: O(1)
In above methods of initialization, the values should be known before the compilation of the program. But we can also initialize the 3D array at runtime using loops. We use three nested loops to go to each element of the array and initialize it to some value. This value is generally taken as input from some source such as file or generated from some series or copied from other arrays.
C Program to Initialize 3D Array Using Loops C
// C Program to illustrate how to initialize
// a 3D array using loops
#include <stdio.h>
int main() {
// Defining a 2x3x2 3D array
int arr[2][3][2];
// Variable to assign values to array elements
int count = 1;
// Initializing the array using loops
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 2; k++)
arr[i][j][k] = count++;
// Printing the 3D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12
Time Complexity: O(d * m * n), where d is the number of 2D arrays (or depth), m and n are the number of rows and columns in each 2D array.
Auxiliary Space: O(1)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4