Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
How to Initialize a Dynamic Array in C?
Next article icon

How to Initialize a 3D Array in C?

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Table of Content

  • Initialization Using Initializer List
  • Zero Initialization
  • Runtime Initialization Using Loops

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

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

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

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

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

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

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)



Next Article
How to Initialize a Dynamic Array in C?

A

amritkumasakv
Improve
Article Tags :
  • C Programs
  • C Language
  • C-Arrays
  • C Examples

Similar Reads

  • How to Initialize a 2D Array in C?
    In C, a 2D Array is a type of multidimensional array in which data is stored in tabular form (rows and columns). It has two dimensions so it can store the data and can expand in two directions. In this article, we will learn how to initialize a 2D array in C. There are three main ways to initialize
    4 min read
  • How to Initialize Array to 0 in C?
    Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so. Initialize Array to 0 in CThere are mainly two ways to ini
    2 min read
  • How to Initialize a Dynamic Array in C?
    In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C. Initializing a Dynamic Arrays
    2 min read
  • How to Initialize Array of Structs in C?
    In C, arrays are data structures that store the data in contiguous memory locations. While structs are used to create user-defined data types. In this article, we will learn how to initialize an array of structs in C. Initializing Array of Structures in CWe can initialize the array of structures usi
    2 min read
  • How to Initialize Array of Pointers in C?
    Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe
    2 min read
  • How to Initialize Char Array in Struct in C?
    In C++, we can also define a character array as a member of a structure for storing strings. In this article, we will discuss how to initialize a char array in a struct in C. Initialization of Char Array in Struct in CWhen we create a structure instance (variable) in C that includes a char array, we
    2 min read
  • How to store words in an array in C?
    We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
    2 min read
  • How to Find the Size of an Array in C?
    The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C. The simplest method to find the size of an array in C is by using sizeof operator. First determine
    2 min read
  • How to Pass a 3D Array to a Function in C?
    A 3D array (or three-dimensional array) in C is a multi-dimensional array that contains multiple layers of two-dimensional arrays stacked on top of each other. It stores elements that can be accessed using three indices: the depth index, row index, and column index. In this article, we will learn ho
    3 min read
  • How to Access Array of Structure in C?
    In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences