Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C Arrays 1D, 2D, Multi dimensional: Tutorial, Examples, FAQs & Interview Tips

1D Arrays

An array is a collection of elements of the same data type stored in contiguous memory locations. Array indices start at 0.

// Declaration
int numbers[5];

// Declaration with initialization
int numbers[5] = {10, 20, 30, 40, 50};

// Size inferred from initializer
int numbers[] = {10, 20, 30, 40, 50};

// Access element
printf("%d", numbers[0]);  // 10
numbers[2] = 99;           // modify element

2D Arrays (Matrices)

A 2D array is an array of arrays - essentially a matrix with rows and columns.

// Declaration: rows x columns
int matrix[3][4];

// Initialization
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// Access: matrix[row][col]
printf("%d", matrix[1][2]);  // 6
1D Array - Declare, Initialize, Traverse
#include <stdio.h>

int main() {
    int scores[] = {85, 92, 78, 95, 88};
    int n = sizeof(scores) / sizeof(scores[0]);  // number of elements = 5

    // Traverse and print
    printf("Scores: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", scores[i]);
    }
    printf("\n");

    // Find sum and average
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += scores[i];
    }
    printf("Sum: %d\n", sum);
    printf("Average: %.2f\n", (float)sum / n);

    // Find maximum
    int max = scores[0];
    for (int i = 1; i < n; i++) {
        if (scores[i] > max) max = scores[i];
    }
    printf("Max: %d\n", max);

    return 0;
}

/*
Scores: 85 92 78 95 88
Sum: 438
Average: 87.60
Max: 95
*/
2D Array - Matrix Addition
#include <stdio.h>

int main() {
    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int b[2][3] = {{7, 8, 9}, {10, 11, 12}};
    int c[2][3];

    // Matrix addition
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            c[i][j] = a[i][j] + b[i][j];
        }
    }

    // Print result matrix
    printf("Matrix A + B:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%4d", c[i][j]);
        }
        printf("\n");
    }

    return 0;
}

/*
Matrix A + B:
   8  10  12
  14  16  18
*/
Passing Array to Function - Find Max Element
#include <stdio.h>

// Arrays are always passed by reference (pointer to first element)
int findMax(int arr[], int size) {
    int max = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > max) max = arr[i];
    }
    return max;
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

// Passing 2D array to function
void print2D(int rows, int cols, int arr[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%4d", arr[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int nums[] = {34, 12, 67, 45, 89, 23};
    int n = sizeof(nums) / sizeof(nums[0]);

    printf("Array: ");
    printArray(nums, n);
    printf("Max element: %d\n", findMax(nums, n));

    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    printf("\n2D Array:\n");
    print2D(2, 3, matrix);

    return 0;
}

/*
Array: 34 12 67 45 89 23
Max element: 89

2D Array:
   1   2   3
   4   5   6
*/

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.