C Array

Summary: in this tutorial, you will learn about the C array, how to declare arrays and how to manipulate array’s elements effectively.

Introduction to C Arrays

An array is a data structure that consists of items of the same type. Technically speaking, an array is a group of contiguous memory locations that have 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 that specifies the number of elements the array can hold.

Here’s the syntax for declaring an array:

type array[size];
Code language: C++ (cpp)

The following example declare an array that hold 5 integers:

int a[5];
Code language: C++ (cpp)

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]
Code language: C++ (cpp)

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

array_name[index] = value;
Code language: C++ (cpp)

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; }
Code language: C++ (cpp)

How it works.

First, declare the size of the array using the SIZE constant:

const int SIZE = 10; // size of the array
Code language: C++ (cpp)

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

int a[SIZE];
Code language: CSS (css)

The array a can hold up to 10 integers.

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

int i; for (i = 0; i < SIZE; i++) a[i] = i;
Code language: C++ (cpp)

Finally, display the array elements:

for (i = 0; i < SIZE; i++) printf("%d ", a[i]);
Code language: C++ (cpp)

Initializing an array

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

int i = 0;
Code language: C++ (cpp)

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, ...};
Code language: C++ (cpp)

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};
Code language: C++ (cpp)

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};
Code language: C++ (cpp)

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; }
Code language: C++ (cpp)

Output:

1 0 0
Code language: C++ (cpp)

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

C array example

The following program allows you to enter 10 numbers, assigns 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; }
Code language: C++ (cpp)

How it works.

First, declare an array of 10 integers:

// declare the number arrays const int SIZE = 10; int numbers[SIZE];
Code language: C++ (cpp)

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]);
Code language: C++ (cpp)

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);
Code language: C++ (cpp)

Summary

  • An array is a data struture that hold 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 tutorial helpful ?