Table of contents
1.
Introduction
2.
Properties of Array
3.
How do you Initialize an Array in C?
4.
Methods to initialize an array in C
4.1.
Method 1: Initialize an array using an Initializer List
4.2.
Method 2: Initialize an array in C using a for loop
4.3.
Method 3: Using Designated Initializers (For gcc compiler only)
5.
How to Declare an Integer Array in C Programming
6.
How to Initialize an Integer Array in C Programming
7.
How to Access Items in an Integer Array in C Programming
7.1.
C
8.
How to Change Items in an Integer Array in C Programming
8.1.
C
9.
Advantage of C Array
10.
Disadvantage of C Array
11.
Frequently Asked Questions
11.1.
Q. How to initialise an array in C? 
11.2.
Q. How to initialize an array of structures in C? 
11.3.
Q. How to initialize string array in C? 
11.4.
Q. How to initialize char array in C code?
12.
Conclusion
Last Updated: Mar 27, 2025
Medium

How do you Initialize an Array in C

Author Sneha Mallik
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Initializing an array in C is one of the fundamental concepts in programming. Arrays are a crucial data structure. It allows us to store multiple values of the same type in a single variable. This makes them essential for handling the collection of data. Understanding how to properly initialize an array is key to effectively using arrays in our programs. In this blog, we will be discussing how to initialize an array in C.

How do you Initialize an Array in C

Also see: C Static Function, Tribonacci Series

Properties of Array

The properties of the array are:

  • Fixed Size: An array has a predefined size, which means it can hold a specific number of elements. The size of an array is determined at the time of its creation and cannot be changed dynamically.
  • Contiguous Memory Allocation: All elements of an array are stored in contiguous memory locations. This allows for efficient access to elements using their indices.
  • Homogeneous Elements: An array stores elements of the same data type. This ensures that each element in the array occupies the same amount of memory.
  • Indexed Access: Elements in an array can be accessed directly using their index, with the first element having an index of 0. This allows for quick retrieval and modification of elements.
  • Sequential Storage: The elements are stored sequentially, meaning that elements are stored one after another in memory, which makes operations like traversing more straightforward.
  • Low Memory Overhead: Since arrays store elements of the same type, they have a low memory overhead compared to other data structures like linked lists, which require additional memory for pointers.

How do you Initialize an Array in C?

Every data structure needs to be initialized. We initialize an Array with {}, which contains expressions separated by commas. We will declare a one-dimensional array like the following:

type array_name [size_of_array];


The “size_of_array” should be constant and greater than zero. We can give any name to our array in place of “array_name”. The “type” of the array can be initialized as “int”, “float”, “char”, etc.

Learn how to Initialize an Array

We can initialize an array in C through two types:

  • Compile time: When we set up an array in C at compile time, we decide on the array's size and values while writing the code. This is like planning the array before the program starts running. It's good when we know exactly how big the array should be and what values it should have from the beginning.
  • Runtime: In C, we can create an array during the program's run, like making it up as we go. We decide the size and values while the program is running. This is useful when we need to know the size or values ahead and need to figure it out as we get more information during the program's operation. We use loops or input commands to set up the array step by step.

Methods to initialize an array in C

Method 1: Initialize an array using an Initializer List

An initializer list is a way to fill an array with values when creating it. When declaring the array, you can provide values enclosed in curly braces {}. 

For example:

int arr[7] = {1, 2, 3, 4, 5, 6, 7};

This sets the elements of arr to the values provided in the list.

Method 2: Initialize an array in C using a for loop

You can initialize an array using a for loop. This method is useful when you want to calculate values dynamically. 

For example:

int arr[6]; for (int i = 0; i < 6; i++) { arr[i] = i + 1; }

This loop assigns values to arr elements based on the loop counter.

Method 3: Using Designated Initializers (For gcc compiler only)

In some C compilers, The designated initializer is a feature like GCC, which allows initializing specific array elements by specifying their indices.

For example:

int arr[4] = [0] 1, [1] 2, [2] 3, [3] 4;

This initializes arr elements directly by indicating their indices and values.

How to Declare an Integer Array in C Programming

In C programming, declaring an integer array involves specifying the data type (int), the array name, and the size of the array within square brackets. Here's the syntax:

int array_name[size];

 

Where, int specifies the data type of the array elements (in this case, integers). array_name is the identifier for the array. size is the number of elements in the array. For example, to declare an integer array named myArray with 5 elements:

int myArray[5];

 

This declaration allocates memory for five integers but does not initialize their values. To initialize the values of the array, you need to explicitly assign values to each element after declaration.

How to Initialize an Integer Array in C Programming

Initializing an integer array in C involves assigning specific values to each element of the array after its declaration. Here's how you can do it:

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

 

Where, array_name is the identifier for the array. size is the number of elements in the array. value1, value2, ..., valueN are the initial values assigned to the elements of the array. For example, to initialize an integer array named myArray with values 1, 2, 3, 4, and 5:

int myArray[5] = {1, 2, 3, 4, 5};

 

You can also initialize only some elements of the array, and the remaining elements will be automatically initialized to zero.

How to Access Items in an Integer Array in C Programming

Accessing items in an integer array in C involves referencing the array name followed by the index of the element you want to access within square brackets. Here's how it's done:

array_name[index]

 

Where, array_name is the identifier for the array. index is the position of the element you want to access. Array indices start from 0. For example, to access the third element of an integer array named myArray:

int value = myArray[2];

 

This retrieves the value stored at the third position (index 2) of the array myArray and assigns it to the variable value.

Accessing items in an integer array in C involves using the array's index. Each element in an array can be accessed using its index, where indexing starts at 0. Let us understand this with the help of an example:

  • C

C

#include <stdio.h>

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

// Access and print each element of the array
printf("Element at index 0: %d\n", numbers[0]); // Output: 10
printf("Element at index 1: %d\n", numbers[1]); // Output: 20
printf("Element at index 2: %d\n", numbers[2]); // Output: 30
printf("Element at index 3: %d\n", numbers[3]); // Output: 40
printf("Element at index 4: %d\n", numbers[4]); // Output: 50

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

How to Change Items in an Integer Array in C Programming

Changing items in an integer array in C involves assigning a new value to the element at a specific index. Here's how you can do it:

array_name[index] = new_value;

 

Where, array_name is the identifier for the array. index is the position of the element you want to change. new_value is the value you want to assign to that element. For example, to change the value of the second element in an integer array named myArray to 10:

myArray[1] = 10;

 

This assigns the value 10 to the second element (index 1) of the array myArray.

Changing items in an integer array in C involves directly accessing an element using its index and assigning a new value to it. Let us understand this with the help of an example:

  • C

C

#include <stdio.h>

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

// Print original values
printf("Original values:\n");
for(int i = 0; i < 5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}

// Change items in the array
numbers[0] = 100; // Change first element
numbers[2] = 300; // Change third element
numbers[4] = 500; // Change fifth element

// Print updated values
printf("\nUpdated values:\n");
for(int i = 0; i < 5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Original values:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

Updated values:
numbers[0] = 100
numbers[1] = 20
numbers[2] = 300
numbers[3] = 40
numbers[4] = 500

Must Read Decision Making in C

Advantage of C Array

The advantages of C array are:

  • Efficient Memory Usage: Arrays use contiguous memory allocation, which allows efficient access to elements and minimizes overhead.
  • Fast Access: Accessing elements by index is very fast, as it involves simple arithmetic to compute the address of the element.
  • Ease of Use: Arrays are straightforward to declare and use, making them an essential and easy-to-understand data structure for beginners.
  • Predictable Performance: Since arrays are stored in contiguous memory, their performance is predictable and less affected by fragmentation.
  • Compatibility: Arrays are a fundamental data structure supported by most programming languages, making it easier to transfer knowledge between languages.

Disadvantage of C Array

The disadvantages of C array are:

  • Fixed Size: The size of an array is fixed at the time of declaration and cannot be changed dynamically, leading to potential wasted space or insufficient capacity.
  • Lack of Flexibility: Arrays do not support dynamic resizing. If you need a data structure that can grow or shrink, you’ll need to use other structures like linked lists or dynamic arrays.
  • Index Out of Bounds: Accessing an array element with an out-of-bounds index can lead to undefined behavior and potential program crashes.
  • No Built-in Bounds Checking: C does not provide automatic bounds checking for arrays, which can lead to bugs and security vulnerabilities if not handled carefully.
  • Memory Allocation: Large arrays can lead to significant memory usage and may cause stack overflow if allocated on the stack rather than the heap.

Frequently Asked Questions

Q. How to initialise an array in C? 

In C, you can initialize an array by specifying the data type, followed by the array name and the initial values enclosed in curly braces. For example: int array[5] = {1, 2, 3, 4, 5};.

Q. How to initialize an array of structures in C? 

To initialize an array of structures in C, you declare an array of structure type and provide the initial values for each structure element within curly braces. Example: struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}};.

Q. How to initialize string array in C? 

To initialize a string array in C, you declare an array of character arrays (strings) and assign each string value within double quotes. Example: char names[3][10] = {"Rahul", "Amit", "Aman"};.

Q. How to initialize char array in C code?

In C, you can initialize a char array by specifying the array name, followed by the initial characters enclosed in double quotes. Example: char letters[5] = {'a', 'b', 'c', 'd', 'e'};.

Conclusion

In this article, we covered how to Initialize Array in C. It is a crucial step in managing and utilizing collections of data effectively within your programs. By understanding the various methods of initialization—such as static initialization, dynamic initialization, and partial initialization—you can ensure that your arrays are set up correctly and efficiently.

If you would like to learn more similar to this topic, check out our related articles on-


Check out the following problems - 

Live masterclass