Arrays in C Programming - Codingque Arrays in C

Welcome to C Programming Tutorial

Your guide to getting started with C programming.

Array

1. Single-Dimensional Arrays

Definition: A single-dimensional array is a collection of elements that are of the same type, stored in contiguous memory locations. You can think of it as a list of values.

Declaration and Initialization: To declare an array, specify the type of its elements and the number of elements it can hold. Initialization can be done at the time of declaration or afterward.

#include <stdio.h>

int main() {
    // Declaration and initialization of an array
    int numbers[5] = {1, 2, 3, 4, 5};
    
    // Accessing and modifying array elements
    printf("First element: %d\\n", numbers[0]); // Output: 1
    numbers[2] = 10; // Modifying the third element
    printf("Third element after modification: %d\\n", numbers[2]); // Output: 10
    
    // Loop through the array
    printf("Array elements: ");
    for(int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\\n");
    
    return 0;
}

Detailed Explanation:

Header File Inclusion: This line includes the standard input/output header file, which is necessary for using functions like printf.

Main Function: The main function is the entry point of the program. It is where execution begins.

Array Declaration and Initialization: This declares an array named numbers that can hold 5 integers. The array is initialized with the values {1, 2, 3, 4, 5}, so numbers[0] is 1, numbers[1] is 2, and so on.

Accessing and Modifying Array Elements: The printf function prints the first element of the array, which is 1 (since numbers[0] is 1). The value of the third element is modified from 3 to 10, and the updated value is printed.

Loop Through the Array: A for loop iterates from 0 to 4 (since the size of the array is 5) and prints the value of each element in the array, separated by spaces. A newline is printed after the loop to format the output neatly.

Return Statement: This returns 0 to the operating system, indicating that the program executed successfully.

2. Multi-Dimensional Arrays

Definition: Multi-dimensional arrays are arrays of arrays. They can be used to represent matrices or tables of data.

Declaration and Initialization: To declare a two-dimensional array, specify the number of rows and columns. Initialization can be done similarly to single-dimensional arrays.

#include <stdio.h>

int main() {
    // Declaration and initialization of a 2D array
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    // Accessing elements
    printf("Element at position (1,1): %d\\n", matrix[1][1]); // Output: 5
    
    // Loop through the 2D array
    printf("Matrix elements:\\n");
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\\n");
    }
    
    return 0;
}

Detailed Explanation:

Header File Inclusion: This line includes the standard input/output header file, necessary for using the printf function.

Main Function: The main function is the starting point of the program.

2D Array Declaration and Initialization: This declares a 2D array named matrix with 3 rows and 3 columns. The array is initialized with the following values:

1 2 3
4 5 6
7 8 9

The array matrix is essentially a table or grid of numbers.

Accessing an Element: The printf function prints the element at the position (1,1) of the matrix. In a 2D array, indices start from 0, so matrix[1][1] refers to the element in the second row and second column, which is 5.

Loop Through the 2D Array: An outer for loop iterates through each row of the matrix, while an inner for loop iterates through each column of the current row. Each element is printed, followed by a space, and a newline is printed after each row to move to the next row.

Return Statement: This statement returns 0 to the operating system, indicating that the program executed successfully.

Previous Next
Modern Footer