Skip to content

C_Arrays.c

SunilOS edited this page Sep 9, 2024 · 2 revisions

Here’s your C code with added comments explaining each part:

#include <stdio.h>  // This header file is included to use the printf function

int main() {
    // Declare and initialize an array 'arr' with 3 elements
    int arr[3] = {1, 2, 3};

    // Loop through the array and print each element
    for (int i = 0; i < 3; i++) {
        // Print the current index and its corresponding value in the array
        printf("arr[%d]: %d\n", i, arr[i]);
    }

    // Return 0 to indicate that the program executed successfully
    return 0;
}

Explanation:

  • #include <stdio.h>: This includes the standard I/O library needed for the printf function.
  • int arr[3] = {1, 2, 3};: Declares an array of 3 integers and initializes it with values 1, 2, and 3.
  • for (int i = 0; i < 3; i++): A loop that iterates through each element of the array.
  • printf("arr[%d]: %d\n", i, arr[i]);: Prints the index and value of each element in the array.

Clone this wiki locally