Back to C Programming
2026-07-145 min read

Initializing an array (C Programming)

Learn Initializing an array (C Programming) step by step with clear examples and exercises.

Why This Matters

Understanding how to initialize arrays is crucial for any C programmer as it forms the foundation for working with more complex data structures and algorithms. Initializing arrays correctly helps ensure that your code runs efficiently and produces accurate results. This knowledge can help you solve real-world problems, prepare for coding interviews, and even debug common mistakes that might arise during your programming journey.

Prerequisites

Before diving into initializing arrays in C, ensure that you have a good understanding of the following concepts:

  1. Basic C syntax: variables, operators, control structures (if-else, for loops)
  2. Data types: int, float, char, etc.
  3. Memory management in C: understanding pointers and dynamic memory allocation
  4. Basic input/output operations: using printf, scanf, and their formats
  5. Understanding of functions and function prototypes
  6. Control structures like switch-case and nested loops
  7. Basic concepts of algorithms and data structures

Core Concept

Declaring an Array

To declare an array in C, you first specify the data type followed by the variable name and square brackets containing the size of the array. For example, to create an array of 5 integers, you would write:

int myArray[5];

Each element in the array is indexed starting from 0, so myArray[0] refers to the first element, myArray[1] refers to the second element, and so on.

Initializing an Array

You can initialize an array by assigning values to its elements directly during declaration using initializer lists. The initializer list consists of a comma-separated list of values enclosed within curly braces {}. For example:

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

In this case, the first element (myArray[0]) will be assigned the value 1, the second element (myArray[1]) will be assigned the value 2, and so on. If you provide fewer values in the initializer list than the array size, the remaining elements will be initialized to zero by default.

Dynamic Array Initialization

In some cases, you may not know the exact size of an array at compile-time. In such situations, you can use variable-length arrays (VLAs) to dynamically allocate memory for arrays during runtime. However, VLAs are C99 extensions and may not be supported by all compilers.

#include <stdio.h>
#include <stdlib.h>

int max(int arr[], int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int *myArray = (int *)malloc(n * sizeof(int));

// Initialize the array using a loop
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &myArray[i]);
}

int maxVal = max(myArray, n);
printf("The maximum value in the array is: %d\n", maxVal);

free(myArray);
return 0;
}

In this example, we dynamically allocate memory for an array of n integers using malloc(). We then initialize the elements by taking user input and print the maximum value in the array. Don't forget to free the allocated memory once you're done with it using free(myArray).

Multidimensional Arrays

C also supports multidimensional arrays, which can be initialized similarly to one-dimensional arrays. For example:

int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

In this case, matrix[0][0] refers to the first element of the first row (which is 1), matrix[1][1] refers to the second element of the second row (which is 6), and so on.

Worked Example

Let's create a simple C program that initializes a two-dimensional array of 3x4 integers, calculates their sum, and prints the result:

#include <stdio.h>

int main() {
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

int sum = 0;

// Calculate the sum of the elements in the array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
sum += matrix[i][j];
}
}

printf("The sum of the elements in the array is: %d\n", sum);
return 0;
}

When you run this program, it should output:

The sum of the elements in the array is: 108

Common Mistakes

  1. Forgetting to initialize an array: If you declare an array without initializing it, all its elements will be set to zero by default. However, this might lead to unexpected behavior if you assume that the array contains specific values.
  1. Incorrect indexing: Remember that array indices start from 0, not 1. Using an index greater than or equal to the size of the array will result in undefined behavior.
  1. Mixing declaration and initialization: It's common to see beginners write code like this:
int myArray[5];
myArray = {1, 2, 3, 4, 5}; // Incorrect!

To correctly initialize an array, you should declare and initialize it on the same line or use a separate assignment statement:

int myArray[5] = {1, 2, 3, 4, 5}; // Correct!
  1. Incorrectly handling multidimensional arrays: It's essential to be aware of the number of dimensions when accessing elements in a multidimensional array. For example, matrix[i][j] is correct for a two-dimensional array, but matrix[i*j] would not work as expected.

Practice Questions

  1. Write a program that initializes an array of 10 integers and finds the maximum value in the array using a function.
  2. Create a program that takes an array size and elements as input, calculates their sum using dynamic memory allocation, and prints the result.
  3. Implement a function that sorts an array of integers using bubble sort. Initialize the array with random values and print the sorted array.
  4. Write a program that initializes a two-dimensional array of 3x4 integers and finds the average of each row and column.
  5. Implement a function that transposes a given two-dimensional array (swaps rows and columns).

FAQ

  1. Why do we need to initialize arrays in C?

Initializing arrays in C helps set specific values for each element, which can be useful when working with data structures or performing calculations. It also makes your code more readable and easier to debug.

  1. Can I declare an array without specifying its size at compile-time?

Yes, you can use variable-length arrays (VLAs) to dynamically allocate memory for arrays during runtime. However, VLAs are C99 extensions and may not be supported by all compilers.

  1. What happens if I try to access an array element with an index greater than its size?

Accessing an array element with an index greater than its size will result in undefined behavior, which can lead to segmentation faults or unpredictable results.

  1. How do I handle multidimensional arrays in C?

To access elements in a multidimensional array, use multiple indices separated by commas. For example, matrix[i][j] for a two-dimensional array with i as the row index and j as the column index.

  1. What is the difference between initializing an array using curly braces and assignment statements?

Initializing an array using curly braces during declaration sets specific values for each element, while using an assignment statement after declaration assigns new values to existing elements without affecting their original values.