1D array initialization in C programming with example

Array Initialization Methods in C Programming

The diagram below illustrates the different array initialization methods used in C programming. Let's explore each method in detail.

1. Array Initialization with Declaration

This is the method of filling an array with values at the time of array creation. Users can provide values enclosed in curly braces {}. This method allows the user to initialize the array at the moment of declaration using an initializer list. An initializer list is a collection of values enclosed within curly braces {} and separated by commas.

Syntax for array initialization:

data_type array_name[size] = {value1, value2, ... valueN};

Example:

// C Program to demonstrate array initialization
#include <stdio.h>

int main() {
    // Array initialization using initializer list
    int no[5] = { 110, 200, 130, 400, 50 };
    
    printf("\n Value of array at third place=%d", no[2]);
    return 0;
}

2. Array Initialization with Declaration without Size

This method is similar to the previous one, but you omit the array size during declaration. The compiler automatically calculates the size based on the number of elements provided in the initializer list.

Syntax:

data_type array_name[] = {v1, v2, v3, v4, v5};

In the syntax above, the size of the array is 5, which is automatically calculated by the compiler. The compiler allocates memory exactly equal to the number of values specified inside the curly braces {}.

The following example demonstrates how to declare and initialize an integer array emp_salary without specifying the size:

// Array initialization with Declaration without size
#include <stdio.h>

int main() {
    // Size is determined by the number of elements: 5
    int emp_salary[] = {8000, 12000, 13000, 4000, 5000};
    
    printf("\nThird salary = %d", emp_salary[2]);
    return 0;
}
Output:
Third salary = 13000

3. Array Initialization After Declaration (Using Loops)

You can initialize an array using a for loop. This method iterates from index 0 to (size - 1), filling the array dynamically. This approach is particularly useful when you need to calculate values at runtime rather than hard-coding them.

#include <stdio.h>

int main() {
    // Declare an array
    int salary[5];
    int i;

    // Initialize array using a "for" loop
    for(int i = 0; i < 5; i++) {
        salary[i] = 1300 * i;
    }
    
    return 0;
}

Explanation:

In the program above, the array salary of size 5 is declared first. The for loop then iterates from 0 to 4, calculating the value for each index.

After execution, the array salary will contain the following values:

salary = {0, 1300, 2600, 3900, 5200}

4. Array Initialization by Specifying Size

In C programming, you can create an array by specifying its size and assigning elements at the time of declaration. This method differs from the previous ones because if you initialize fewer values than the declared size, the compiler automatically initializes the remaining elements to 0.

Syntax:

// Declare an array by specifying size and initializing at declaration
int salary[4] = {100, 200, 300, 400};

// Array with fewer values than size
int salary2[4] = {100, 200, 300};

In the example above, salary is an array of size 4 where all four elements are explicitly initialized. However, salary2 is also size 4 but has only three values provided. The compiler automatically sets the fourth element to 0.


5. Getting Input from the User

In C programming, you can populate an array at runtime by taking input from the user. This is typically done using a for loop combined with the scanf() function. The loop iterates through each index of the array, storing the user's input sequentially.

#include <stdio.h>

int main() {
    int salary[5];
    int i;

    printf("\nEnter five different salaries:\n");
    
    // Loop 1: Get input from user and store in array
    for(i = 0; i < 5; i++) {
        scanf("%d", &salary[i]);
    }

    printf("\nThe Salaries Are:\n");
    
    // Loop 2: Display the stored values
    for(i = 0; i < 5; i++) {
        printf("%d\n", salary[i]);
    }

    return 0;
}

In the example above, the first for loop runs 5 times, asking the user for input and storing it in salary[i]. The second loop iterates through the array to print the values back to the screen to verify the input.



6. Initialization with Specific Indices (Mixed)

In C, you can mix designated initialization with sequential assignment. This allows you to set specific indices explicitly and let the compiler fill the subsequent indices automatically.

// Example: Designating specific indices and filling the rest sequentially
int salary[5] = { [4] = 5000, [0] = 1200, 2560, 3200, 4500 };

In the example above, index 4 is explicitly set to 5000. Then, index 0 is set to 1200. Because the list continues after [0], the compiler automatically assigns the next values (2560, 3200, 4500) to indices 1, 2, and 3 respectively.


7. Using Designated Initializers (C99 Standard)

This method allows you to initialize specific array elements by explicitly stating their indices. This is the clearest way to initialize an array non-sequentially, ensuring you don't accidentally mix up the order of values.

int salary[4] = { [0] = 1500, [1] = 2000, [2] = 5400, [3] = 6500 };

This initializes the salary array elements directly by specifying their index and corresponding value. Any indices not specified (if the array size is larger than the list) are automatically initialized to 0.

Previous Topic:-->> Declare 1D array in C || Next topic:-->>Access element 1D array.