C Programming Tutorial 83 of 206
Introduction & Setup
- Introduction to C
- History of C
- Features of C
- Applications of C
- Advantages and Limitations of C
- C Standards and Implementations
- How C Programs Run
- Setting Up C Development Environment
- Your First C Program
- Basic Syntax of C
- Structure of a C Program
- Compilation Process in C
Tokens & Basics
- Tokens in C
- Keywords in C
- Identifiers in C
- Constants in C
- Variables in C
- Scope Rules in C
- Storage Classes in C
- Comments in C
Data Types & I/O
- Data Types in C
- Primitive Data Types
- Derived Data Types
- Type Modifiers
- Type Conversion in C
- Type Casting in C
- sizeof Operator
- Input and Output in C
- printf() in C
- scanf() in C
- Character I/O: getchar and putchar
- String I/O: gets/fgets and puts
- Format Specifiers in C
Operators & Expressions
- Operators in C
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment and Decrement Operators
- Conditional Operator
- Comma Operator
- sizeof and Address Operators
- Operator Precedence and Associativity
- Expression Evaluation in C
- Short-Circuit Evaluation
Control Flow
- Decision Making in C
- if Statement
- if-else Statement
- else-if Ladder
- Nested if
- switch Statement
- Loops in C
- for Loop
- while Loop
- do-while Loop
- Nested Loops
- break Statement
- continue Statement
- goto Statement
Functions
- Functions in C
- Function Declaration and Definition
- Function Arguments
- Return Values
- Call by Value
- Call by Reference Using Pointers
- Parameter Passing Techniques
- Main Function in C
- Recursion in C
- Types of Recursion
- Inline Functions
- Nested Functions
- Function Pointers
- Variadic Functions
Arrays
- Arrays in C
- Declaration and Initialization of Arrays
- Accessing Array Elements
- Traversing an Array
- Passing Arrays to Functions
- Array Parameters and Pointers
- One-Dimensional Arrays in C
- Two Dimensional Arrays
- Multidimensional Arrays in C
- Passing 2D Arrays to Functions
- Array of Pointers
- Variable Length Arrays
Strings
- Strings in C
- String Declaration and Initialization
- String Length
- String Traversal
- String Comparison
- String Copy
- String Concatenation
- Substring Search
- String Tokenization
- String Reverse
- Palindrome String in C
- String Pattern Searching
- String Literals
Pointers
- Pointers in C
- Address and Dereference Operators
- Pointer Arithmetic
- Pointers and Arrays
- Pointer to Pointer
- Void Pointers
- Null Pointers
- Dangling Pointers
- Wild Pointers
- Constant Pointers and Pointer to Constant
- Function Pointers
- Array of Pointers
- Pointers to Functions
- Pointers to Structures
- Pointer Type Casting
Structures & Unions
- Structures in C
- Structure Members
- Array of Structures
- Nested Structures
- Pointers to Structures
- Passing Structures to Functions
- Structure Alignment and Padding
- Self-Referential Structures
- Bit Fields in Structures
- Unions in C
- Structure vs Union
- Enumeration in C
- typedef in C
Dynamic Memory & Memory
- Program Memory Layout in C
- Stack and Heap in C
- malloc()
- calloc()
- realloc()
- free()
- Dynamic Arrays
- Memory Leaks
- Dangling Memory
- Double Free
- Use After Free
- Memory Safety Best Practices
Preprocessor & Headers
- C Preprocessor
- Header Files in C
- #include Directive
- #define and Macros
- Function-like Macros
- Conditional Compilation
- #ifdef, #ifndef and #endif
- Predefined Macros
- Pragma Directive
- Include Guards
File Handling
- File Handling in C
- File Pointers
- fopen() and fclose()
- Reading a File
- Writing to a File
- Appending to a File
- fgetc() and fputc()
- fgets() and fputs()
- fprintf() and fscanf()
- Binary File Handling
- EOF, getc() and feof()
- Read/Write Structures to Files
- Delete a File
- File Error Handling
Error Handling & Debugging
- Error Handling in C
- perror()
- strerror()
- errno
- Assertions in C
- Debugging C Programs
- Common C Runtime Errors
- Divide by Zero Handling
- goto for Error Handling
Date, Time & Misc
- Date and Time in C
- time() Function
- clock() Function
- Random Numbers in C
- Linkage in C
- External and Static Linkage
- Storage Duration
- Integer Overflow and Undefined Behavior
- Implementation-Defined vs Undefined Behavior
Advanced C
- Generic Selection (_Generic)
- Variadic Functions
- Input/Output System Calls
- Signals in C
- Signal Handling
- Socket Programming in C
- Multithreading in C
- Threads and Synchronization
- Atomic Operations
- Memory-Mapped Files
- Low-Level Systems Programming
Practice & Projects
- C Programming Examples
- C Exercises
- C Fundamental Practice Problems
- C Functions Practice Problems
- C Compound Data Type Practice Problems
- C Pattern Programs
- C Array Programs
- C String Programs
- C Pointer Programs
- C Structure and Union Programs
- C File Handling Projects
- C Mini Projects
- C Interview Questions
- Data Structures and Algorithms in C
Multidimensional Arrays in C
Learn how multidimensional arrays work in C, including 2D arrays, declaration, initialization, accessing elements, and practical examples.
On this page
What Are Multidimensional Arrays?
A multidimensional array is an array that contains data in more than one dimension.
The most common type is a two-dimensional (2D) array, which stores data in rows and columns.
For example:
1 2 3
4 5 6
This can be represented as a 2D array with 2 rows and 3 columns.
Declaring a 2D Array
The basic syntax is:
data_type array_name[rows][columns];
Example:
int numbers[2][3];
This creates an integer array with:
- 2 rows
- 3 columns
- 6 total elements
Initializing a 2D Array
You can initialize a 2D array when declaring it:
int numbers[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
The array looks like:
1 2 3
4 5 6
Accessing Elements
Each element is accessed using two indexes:
array[row][column]
Example:
printf("%d", numbers[0][1]);
Output:
2
Array indexes start from 0, so:
numbers[0][0] → 1
numbers[0][1] → 2
numbers[0][2] → 3
numbers[1][0] → 4
numbers[1][1] → 5
numbers[1][2] → 6
Traversing a 2D Array
Nested for loops are commonly used to visit every element.
#include <stdio.h>
int main()
{
int numbers[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", numbers[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 2 3
4 5 6
The outer loop handles the rows, while the inner loop handles the columns.
Taking Input
You can also fill a multidimensional array using user input.
#include <stdio.h>
int main()
{
int numbers[2][3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &numbers[i][j]);
}
}
return 0;
}
Each value is stored at a specific row and column.
Three-Dimensional Arrays
C also supports arrays with three or more dimensions.
Example:
int data[2][3][4];
This represents:
2 × 3 × 4 = 24 elements
Higher-dimensional arrays are useful for representing data with multiple dimensions, although 2D arrays are much more common in beginner programming and DSA.
Common Uses
Multidimensional arrays are commonly used for:
- Matrices
- Tables
- Grids
- Game boards
- Image data
- Mathematical calculations
For example, a matrix can be represented as:
int matrix[3][3];
Important Points
- A 2D array uses
array[row][column]. - Array indexes start at
0. - Nested loops are commonly used to traverse 2D arrays.
int matrix[3][4]contains3 × 4 = 12elements.- C supports arrays with multiple dimensions.
Key Takeaway
A multidimensional array allows you to organize related data across multiple dimensions. The 2D array is the most commonly used form and represents data in rows and columns.
Once you understand 1D arrays, learning 2D arrays mainly involves managing two indexes and nested loops.
Related
-
For Loop
for Loop — part of the C Programming Tutorial.
-
Closest Number
Learn how to find the number closest to a given target by comparing absolute differences, an important pattern in DSA problems.
-
Floyd's Triangle
Learn how to generate Floyd's Triangle using nested loops and understand the row-by-row numbering pattern.
-
While Loop
Learn how the while loop in C repeatedly executes a block of code while a condition remains true.