VOOZH about

URL: https://www.geeksforgeeks.org/c/how-to-initialize-a-3d-array-in-c/

⇱ How to Initialize a 3D Array in C? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Initialize a 3D Array in C?

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 List

We can initialize a 3D array at the time of declaration by providing the values in curly braces {} using the following syntax:

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:

int arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

C Program to Initialize a 3D Array using Initializer List


Output
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)

Zero Initialization

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.

int arr[2][3][2] = { 0 }

C Program to Initialize 3D Array with Zero


Output
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)

Runtime Initialization Using Loops

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


Output
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)


Comment
Article Tags: