Initialization of 3D Array in C

Last Updated : 7 Jul, 2026

A 3-D array is a multidimensional array that stores data in depth, rows, and columns. It has three dimensions, allowing data to be organized in a three-dimensional grid.

  • A 3-D array can be initialized in multiple ways, such as using nested braces, a flat list, or runtime initialization.
  • The initialization method determines how values are assigned to the array elements during declaration.

Example:

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

There are three ways in which a 3D array can be initialized in C

Initialization Using Initializer List

A 3D array can be initialized at the time of declaration by providing values inside curly braces {}. The values are arranged using nested braces to represent each dimension.

  • Each level of nested braces represents a different dimension of the 3-D array.
  • The values are assigned to the array elements in the order they appear in the initializer list.

Syntax

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

  • The outermost braces group all the elements of the 3-D array, while the next level of braces represents each 2-D array (depth level).
  • The innermost braces represent the rows of each 2-D array, and the values are assigned to the elements row by 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};

Example: Program to Initialize a 3D Array using Initializer List

C
#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;
}

Output
0 1 
2 3 
4 5 

6 7 
8 9 
10 11 

Zero Initialization

A 3-D array can be initialized with zero by using {0} during declaration. This initializes all elements of a numeric array to 0.

  • Using {0} automatically sets every element of the 3-D array to zero.
  • This method is applicable to numeric arrays or types that can be converted to numeric values.

Syntax

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

Example: Program to Initialize 3D Array with Zero

C
#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;
}

Output
0 0 
0 0 
0 0 

0 0 
0 0 
0 0 

Runtime Initialization Using Loops

Runtime initialization assigns values to a 3-D array during program execution instead of at compile time. It is useful when values are entered by the user or generated dynamically.

  • It is performed using three nested loops, where the loops traverse the depth, rows, and columns of the array.
  • Runtime initialization is commonly used for user input, generated values, or copying data from other arrays.

Syntax

int arr[2][2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
scanf("%d", &arr[i][j][k]);

Example: Program to Initialize 3D Array Using Loops

C
#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;
}

Output
1 2 
3 4 
5 6 

7 8 
9 10 
11 12 
Comment