C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Arrays and Strings
  5. Types of Array

Types of Array

C supports primarily two types of arrays:

  • Single Dimensional Array (1D Array)
  • Multidimensional Array (including Two Dimensional and Three Dimensional Arrays)

A 1D array is a list of variables of the same type, accessible by a single index.

Syntax:

data_type array_name[size];

Example:

#include <stdio.h>
int main() {
    int numbers[5] = {10, 20, 30, 40, 50};
    
    for (int i = 0; i < 5; i++) {
        printf("Element %d = %d\n", i, numbers[i]);
    }
    return 0;
}

A multidimensional array is an array of arrays. The most commonly used is the two-dimensional array which can be thought of as a matrix (rows and columns).

A) Two-Dimensional Array (2D Array)

Syntax:

data_type array_name[row][column];

Example:

#include <stdio.h>
int main() {
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}

B) Three-Dimensional Array (3D Array)

Syntax:

data_type array_name[x][y][z];

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *