Declare and Initialize an Integer Array in C

In C, an integer array is declared using the int data type followed by the array name and size. It can be initialized at the time of declaration or later in the program.

The elements in the array are stored in contiguous memory locations and can be accessed using index values starting from 0.


Examples of Declaring and Initializing an Integer Array

1. Declaring and Initializing an Integer Array at the Time of Declaration

In this example, we declare an integer array and initialize it with values at the same time. We then print all elements of the array.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare and initialize an integer array
    int numbers[] = {10, 20, 30, 40, 50};

    // Print all elements of the array
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] and initialize it with values {10, 20, 30, 40, 50}.
  2. The size of the array is automatically determined based on the number of elements.
  3. We use a for loop to iterate through the array and print each element.
  4. The loop variable i starts at 0 and goes up to 4, accessing each element using numbers[i].
  5. printf() is used inside the loop to print each value.

Output:

10 20 30 40 50

2. Declaring an Integer Array and Assigning Values Later

In this example, we declare an integer array first and then assign values to each index separately.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare an integer array of size 5
    int numbers[5];

    // Assign values to the array elements
    numbers[0] = 5;
    numbers[1] = 10;
    numbers[2] = 15;
    numbers[3] = 20;
    numbers[4] = 25;

    // Print all elements of the array
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Explanation:

  1. We declare an integer array numbers[5] with a size of 5.
  2. We assign values to each index manually using numbers[index] = value;.
  3. We use a for loop to iterate through the array and print each value.
  4. The loop runs from index 0 to 4, accessing elements using numbers[i].
  5. printf() prints each element in the array.

Output:

5 10 15 20 25

3. Initializing an Array with Zero Values

In this example, we initialize an integer array with all zero values and print them.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare and initialize an integer array with all zeros
    int numbers[5] = {0};

    // Print all elements of the array
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Explanation:

  1. We declare an integer array numbers[5] and initialize it with {0}.
  2. When an array is initialized with a single zero, all other elements are set to zero by default.
  3. We use a for loop to iterate through the array and print each element.
  4. The loop runs from index 0 to 4, accessing each element using numbers[i].
  5. printf() prints each value, which are all zero.

Output:

0 0 0 0 0

Conclusion

In this tutorial, we explored different ways to declare and initialize integer arrays in C:

  1. Declaring and initializing an integer array at the time of declaration.
  2. Declaring an array and assigning values later.
  3. Initializing an array with all zero values.

Arrays provide an efficient way to store and manipulate multiple values in a structured manner. Understanding initialization techniques is essential for effective programming in C.