Skip to content
Laviyx Learn

Try

C course menu

Arrays in C: Declaration, Initialisation, Indexing and Common Operations

Learn arrays in C: declaring and initialising arrays, zero-based indexing, finding the length with sizeof, looping over arrays, searching, finding the largest value, reversing, the danger of out-of-bounds access, and copying arrays.

Beginner7 min readUpdated
On this page

An array stores a fixed number of values of the same type, one after another in memory. Each value is an element, reached by its index, starting at 0.

Declaring and initialising#

C
#include <stdio.h>

int main(void) {
    int marks[5] = {78, 92, 65, 88, 71};
    double prices[] = {49.5, 120.0, 15.25};   /* size worked out: 3 */
    int zeros[4] = {0};                         /* all four are 0 */
    int partial[5] = {1, 2};                    /* rest are 0: 1 2 0 0 0 */

    printf("%d %d\n", marks[0], marks[4]);
    printf("%.2f\n", prices[2]);
    printf("%d %d\n", zeros[3], partial[4]);
    return 0;
}
Output
78 71
15.25
0 0
  • The size must be known when the array is created, and it cannot change afterwards.
  • If you give fewer initial values than the size, the rest are set to 0.
  • A local array with no initialiser contains garbage values.

Length with sizeof#

sizeof gives a size in bytes. Dividing the array's size by one element's size gives the number of elements:

C
int marks[] = {78, 92, 65, 88, 71};
size_t n = sizeof marks / sizeof marks[0];
printf("%zu elements\n", n);
Output
5 elements

This works only on the array itself, not on a pointer or a function parameter (Parameters).

Looping over an array#

C
int marks[] = {78, 92, 65, 88, 71};
int n = sizeof marks / sizeof marks[0];
int total = 0, best = marks[0];

for (int i = 0; i < n; i++) {
    total += marks[i];
    if (marks[i] > best) best = marks[i];
}
printf("total %d, average %.1f, best %d\n", total, (double)total / n, best);
Output
total 394, average 78.8, best 92

Searching#

A linear search checks each element in turn:

C
int ids[] = {104, 230, 517, 342, 999};
int n = 5, target = 342, found_at = -1;

for (int i = 0; i < n; i++) {
    if (ids[i] == target) {
        found_at = i;
        break;
    }
}
printf("found at index %d\n", found_at);
Output
found at index 3

For a sorted array, binary search is much faster (Recursion).

Reversing in place#

Swap the first and last elements, then move inwards:

C
int a[] = {1, 2, 3, 4, 5, 6};
int n = 6;
for (int i = 0, j = n - 1; i < j; i++, j--) {
    int tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
}
for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
Output
6 5 4 3 2 1 

Out-of-bounds access#

C does not check indexes. Reading or writing marks[5] in a 5-element array is undefined behavior: it may print garbage, silently overwrite another variable, or crash. Valid indexes are 0 to n - 1, so loops use i < n, never i <= n.

Copying arrays#

Arrays cannot be assigned with =. Copy element by element, or with memcpy from <string.h>:

C
#include <stdio.h>
#include <string.h>

int main(void) {
    int src[] = {5, 10, 15};
    int dst[3];
    memcpy(dst, src, sizeof src);
    dst[0] = 99;
    printf("%d %d\n", src[0], dst[0]);
    return 0;
}
Output
5 99

Practice#

Practice exercise

Given int temps[] = {31, 28, 35, 33, 29, 36, 30}; (a week of temperatures), print the average to one decimal place, the number of days above the average, and the index of the hottest day.

Show solution
C
#include <stdio.h>

int main(void) {
    int temps[] = {31, 28, 35, 33, 29, 36, 30};
    int n = sizeof temps / sizeof temps[0];

    int total = 0, hottest = 0;
    for (int i = 0; i < n; i++) {
        total += temps[i];
        if (temps[i] > temps[hottest]) hottest = i;
    }
    double avg = (double)total / n;

    int above = 0;
    for (int i = 0; i < n; i++) {
        if (temps[i] > avg) above++;
    }
    printf("average %.1f, days above %d, hottest index %d\n", avg, above, hottest);
    return 0;
}
Output
average 31.7, days above 3, hottest index 5

Key takeaway#

Finished this lesson?