C Array

Summary: In this tutorial, you will learn about C arrays, how to declare them, and how to manipulate their elements effectively.

Introduction to C Arrays #

An array is a data structure consisting of items of the same type. Technically speaking, an array is a group of contiguous memory locations of the same type.

Declare an array #

To define an array, you need to specify:

  1. The type of the elements.
  2. The array name.
  3. The array size specifies the number of elements the array can hold.

Here’s the syntax for declaring an array:

type array[size];

The following example declares an array that holds 5 integers:

int a[5];

When you declare an array, the compiler allocates a memory block that holds the entire array. The array elements are stored sequentially in the memory as shown in the following picture:

Access array elements #

The items in the array are called elements. Each element has an index. The first element has index 0, the second element has index 1, and so on. If an array has n elements, the last element will have the index n - 1.

To access an element in an array, you use the square bracket syntax as follows:

array_name[index]

To assign a value to an array element, you use this syntax:

array_name[index] = value;

The following program demonstrates how to assign integer numbers to elements of an array and display the array elements:

#include <stdio.h>

int main()
{
    const int SIZE = 10; // size of the array
    int a[SIZE];         // an array of 10 integers

    // assign integers to array elements
    int i;
    for (i = 0; i < SIZE; i++)
        a[i] = i;

    // dislay the array
    for (i = 0; i < SIZE; i++)
        printf("%d ", a[i]);

    return 0;
}

How it works.

First, declare the size of the array using the SIZEconstant:

const int SIZE = 10; // size of the array

Second, declare an array of integers with the size specified by the SIZE constant:

int a[SIZE];

The array a can hold up to 10 integers.

Third, assign integers numbers from 0 to 9 to the elements of the array:

int i;
for (i = 0; i < SIZE; i++)
    a[i] = i;

Finally, display the array elements:

for (i = 0; i < SIZE; i++)
    printf("%d ", a[i]);

Initializing an array #

When you declare a variable, you can initialize it to a value. For example:

int i = 0;

Similarly, you initialize an array like a variable. To initialize an array, you specify values enclosed in curly braces in the declaration part like this:

type array[SIZE] = {value1, value2, ...};

The values {value1, value2, …} are often called initializers.

For example, the following declares the array called scores that hold 5 integers from 1 to 5:

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

If you omit the size of the array, C will create an array whose size is the same as the number of initializers:

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

In this example, the size of the scores array is 5.

If you provide fewer initializers, the remaining elements are initialized to zero. For example:

#include <stdio.h>

int main()
{
    int a[3] = {1};

    for (int i = 0; i < 3; i++)
        printf("%d ", a[i]);

    return 0;
}

Output:

1 0 0 

In this example, we declare an array with size 3. However, we initialize it with one value, so the second and third elements are zero.

C array example #

The following program allows you to enter 10 numbers, assign them to the array elements, and calculate the sum of the array elements:

#include <stdio.h>

int main()
{
    // declare the number arrays
    const int SIZE = 10;
    int numbers[SIZE];
    int i;

    // prompt for inputs
    printf("Please enter 10 numbers for the array elements:");
    for (i = 0; i < SIZE; i++)
        scanf("%d", &numbers[i]);

    // calculate the sum of the elements in the array
    int total = 0;
    for (i = 0; i < SIZE; i++)
        total += numbers[i];

    // show the total
    printf("The sum of array elements is %d ", total);

    return 0;
}

How it works.

First, declare an array of 10 integers:

// declare the number arrays
const int SIZE = 10;
int numbers[SIZE];

Second, prompt for input 10 integers and assign each number to the array element:

// prompt for inputs
printf("Please enter 10 numbers for the array elements:");
for (i = 0; i < SIZE; i++)
    scanf("%d", &numbers[i]);

Third, calculate the total of array elements and how the result:

// calculate the sum of the elements in the array
int total = 0;
for (i = 0; i < SIZE; i++)
    total += numbers[i];

// show the total
printf("The sum of array elements is %d ", total);

Summary #

  • An array is a data structure with a fixed number of elements of the same type.
  • Each element in an array is identified by an index. The first element has index 0.
  • Initialize array elements by assigning a comma-separated list of values to the array variable.

Was this helpful?