Back to C Programming
2026-04-096 min read

Array initialization (C Programming)

Learn Array initialization (C Programming) step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on array initialization in C programming! In this tutorial, we'll delve into the practical depth you need for exams, interviews, and real-world coding scenarios. By the end of this tutorial, you'll be well-equipped to handle array initializations confidently. Let's get started!

Why is Array Initialization Important in C Programming?

Array initialization is a fundamental concept in C programming that allows us to set initial values for an array at the time of declaration. Understanding array initialization will help you avoid common pitfalls, write cleaner code, and tackle more complex problems with ease.

Prerequisites

Before diving into array initialization, it's essential to have a good understanding of:

  • Basic C syntax (variables, data types, operators)
  • Arrays in C programming (declaration, accessing elements)
  • Pointers and their relationship with arrays (optional but recommended for advanced topics)

If you're not familiar with these topics, we recommend checking out our C Programming for Beginners tutorial first.

Core Concept

Initializing Arrays

In C programming, arrays can be initialized at the time of declaration with a list of values enclosed in curly braces {}. The number of initializers should match the size of the array. Here's an example:

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

In this example, we declare an array called arr with a size of 5 and initialize it with the values 1 through 5.

Initializing Arrays with Fewer Values Than Elements

If you provide fewer initializers than the number of elements in the array, the remaining elements will be set to zero by default:

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

In this case, arr[0], arr[1], and arr[2] are initialized with 1, 2, and 3, while arr[3] and arr[4] will be set to zero.

Initializing Character Arrays (Strings)

To initialize a character array as a string, you can enclose the string in double quotes:

char str[] = "Hello, World!";

This initializes an array of characters with the ASCII values for each character in the string. The null character \0 is automatically appended at the end to mark the end of the string.

Initializing Multidimensional Arrays

Initializing multidimensional arrays requires providing a list of initializers enclosed in curly braces, with each row separated by commas:

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

In this example, we declare a 2x3 matrix called arr and initialize it with the values 1 through 6.

Initializing Arrays with Variable Length (Variable-Length Arrays or VLAs)

C99 introduced Variable-Length Arrays (VLAs), which allow arrays to be created with sizes specified at runtime:

void readArray(int size, int arr[]) {
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
}

In this example, we define a function readArray() that takes an array of integers with a dynamic size as its argument.

Initializing Arrays with Arithmetic Expressions

You can also initialize arrays using arithmetic expressions:

int arr[5] = {0, 2*i for (int i = 1; i <= 4)};

In this example, we use a for loop to generate the initial values for arr. The resulting array will contain the values 0, 2, 4, 6, and 8.

Worked Example

Let's work through an example to better understand array initialization:

#include <stdio.h>

int main() {
int arr1[3] = {1, 2, 3};
int arr2[5];
char str1[] = "Apple";
char str2[] = "Banana";

for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
printf("%d ", arr1[i]);
}
printf("\n");

// Initialize the remaining elements of arr2 with zeroes
for (int i = 3; i < 5; i++) {
arr2[i] = 0;
}

for (int i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++) {
printf("%d ", arr2[i]);
}
printf("\n");

printf("str1: %s\n", str1);
printf("str2: %s\n", str2);

return 0;
}

In this example, we declare three arrays: arr1, arr2, and two character arrays str1 and str2. We initialize arr1 with the values 1 through 3, but arr2 is left uninitialized. Since we haven't provided enough initializers for arr2, its remaining elements are set to zero by default.

We then print out each array using a loop that iterates through their respective sizes. Finally, we print out the character arrays as strings using the printf function.

Common Mistakes

  1. ### Forgetting Array Size

When initializing an array with fewer values than its size, it's essential to remember the array's size:

int arr[5] = {1, 2, 3}; // Correct
int arr[5] = {1, 2, 3, 4}; // Incorrect - too many initializers
int arr[5]; // Incorrect - no initializers
  1. ### Mixing Up Array and Scalar Initialization

Be careful not to mix up array and scalar initialization:

int arr[] = 10; // Incorrect - trying to initialize an array with a scalar value
int arr[5] = {1, 2, 3, 4, 5}; // Correct - initializing an array with multiple values
  1. ### Initializing Arrays with Non-matching Data Types

Ensure that the data type of the initializers matches the declared data type of the array:

int arr[5] = {1, "Hello"}; // Incorrect - int and char* have different data types
char str[6] = {1, 2, 3}; // Incorrect - char expects ASCII values
  1. ### Initializing Multidimensional Arrays with Inconsistent Rows

Ensure that each row of a multidimensional array has the same number of elements:

int arr[2][3] = {1, 2, 3, 4}; // Incorrect - missing third element in the second row
int arr[2][3] = {{1, 2}, {1, 2, 3}}; // Correct
  1. ### Initializing Arrays with Arithmetic Expressions that have Side Effects

Be careful when using arithmetic expressions in array initialization, as they may have side effects:

int arr[5] = {i++ for (int i = 0; i < 5)}; // Incorrect - i is incremented before being used as an initializer
int arr[5] = {i for (int i = 0; i < 5)}; // Correct - i is not incremented until it's used as an initializer

In this example, using i++ in the array initialization will increment i before it's used as an initializer, resulting in an incorrect array. To avoid this issue, you can use i without the increment operator.

Practice Questions

  1. Write a program that initializes and prints out an array of integers with the values from 0 to 9.
  2. Write a program that initializes two character arrays: one with the word "Coding" and another with the word "Practice". Print both arrays as strings.
  3. Write a program that initializes a 3x3 matrix of integers and prints its contents.
  4. Write a function that takes a Variable-Length Array (VLA) of integers as an argument, calculates their sum, and returns the result.
  5. Write a program that uses arithmetic expressions to initialize an array with the Fibonacci sequence up to the 10th term.

FAQ

### Can I initialize an array with a mix of constants and variables?

Yes, you can initialize an array with a mix of constants and variables:

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

### Can I change the initial values of an initialized array?

Yes, you can modify the initial values of an initialized array like any other variable in C programming:

int arr[5] = {1, 2, 3, 4, 5};
arr[0] = 10; // Now arr[0] is 10

### What happens if I try to initialize an array with more values than its size?

If you provide more initializers than the number of elements in the array, the excess initializers will be ignored:

int arr[5] = {1, 2, 3, 4, 5, 6}; // Incorrect - too many initializers, arr[4] and arr[5] are ignored

### Can I initialize an array with a mix of values and pointers?

Yes, you can initialize an array with a mix of values and pointers, but be aware that the pointer will store its address value:

int arr[5] = {1, &arr[0], 3}; // Incorrect - arr[1] stores the address of arr[0] instead of a value
int arr[5] = {1, (int*)&arr[0], 3}; // Correct - cast the pointer to an int to store its address as a value

In this example, initializing arr[1] with &arr[0] stores the address of arr[0], which is not what we intended. To store the address value as an integer, we can cast the pointer to an int before assigning it to the array.