Skip to main content

Initializing Arrays in C

Array Initialization

Array Initialization in C refers to the process of assigning values to the elements of an array.

  • This can be done at the time of creating the array or at a later point in the program.

  • Array initialization is important because it provides a way to set the initial values of the elements of an array, which can be used later in the program to perform various operations or calculations.

For example:

The following code initializes an array of integers called numbers with the values 10, 20, 30, 40, and 50:

int numbers[] = {10, 20, 30, 40, 50};

Multiple ways to initialize an Array

There are several ways to initialize an array in C:

Using an Initialization List

Here's an example in C that uses an initialization list to initialize an array:

#include <stdio.h>

int main()
{
int numbers[] = {10, 20, 30, 40, 50}; // Initialization list
int size = sizeof(numbers) / sizeof(numbers[0]);

printf("The size of the array is: %d\n", size);
printf("The elements of the array are: ");

for (int i = 0; i < size; i++)
{
printf("%d ", numbers[i]);
}

printf("\n");

return 0;
}
Output:

Example

  • The array numbers is initialized with the values 10, 20, 30, 40, and 50 using an initialization list.
  • The size of the array is determined using the sizeof operator
  • Then used in a for loop to print the elements of the array.

Using a Loop

Here's an example in C that uses a loop to initialize an array:

#include <stdio.h>

int main()
{
int numbers[5]; // Array declaration
int size = sizeof(numbers) / sizeof(numbers[0]);

for (int i = 0; i < size; i++)
{
numbers[i] = i * 2;
}

printf("The size of the array is: %d\n", size);
printf("The elements of the array are: ");

for (int i = 0; i < size; i++)
{
printf("%d ", numbers[i]);
}

printf("\n");

return 0;
}
Output:

Example

  • The array numbers is declared with 5 elements and is then initialized using a loop.
  • The size of the array is determined using the sizeof operator and is then used in a for loop to print the elements of the array.

Using memset Function

Below an example in C that uses memset to initialize an array:

#include <stdio.h>
#include <string.h>

int main()
{
int numbers[5]; // Array declaration
int size = sizeof(numbers) / sizeof(numbers[0]);

memset(numbers, 0, size * sizeof(int));

printf("The size of the array is: %d\n", size);
printf("The elements of the array are: ");

for (int i = 0; i < size; i++)
{
printf("%d ", numbers[i]);
}

printf("\n");

return 0;
}
Output:

Example

  • The array numbers is declared with 5 elements and is then initialized using the memset function.
  • The memset function sets a block of memory to a specific value.
  • The memory block is numbers and the value is 0.
  • The size of the array is determined using the sizeof operator and is then used in a for loop to print the elements of the array.

Declaration vs Initialization (Definition)

Below table summarizes the differences between array declaration and Initialization (Definition):

Array DeclarationArray Definition
PurposeTo create an array and specify its type, name and sizeTo allocate memory for an array and initialize its values
Syntaxdata_type array_name[array_size];data_type array_name[array_size] = {value1, value2, ..., value_n};
Exampleint numbers[10];int numbers[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
Memory AllocationNo memory is allocated for the arrayMemory is allocated for the array and its elements