CIntroduction to Arrays

Introduction to Arrays

An array in C is a fixed-size, contiguous block of memory that holds multiple values of the same type. Instead of declaring five separate int variables to store five scores, you can declare a single array of five ints and access each one by its position, or index. Arrays are one of the most fundamental building blocks in C — strings, rows of a matrix, and countless data structures are all built on top of them.
Declaring an Array

To declare an array you write the element type, a name, and the number of elements in square brackets:

C
int arr[5];       // an array of 5 ints, uninitialized (garbage values)
double prices[10]; // an array of 10 doubles
char letters[26];  // an array of 26 chars
This reserves enough contiguous memory to hold exactly five int values back-to-back. The array itself does not store its own length anywhere — C does not track it for you, which is a recurring theme you will see throughout this section.
Zero-Based Indexing
C arrays are zero-indexed: the first element is at index 0, and the last element of an array with n elements is at index n - 1. This trips up many newcomers coming from 1-indexed languages or everyday counting habits.

C
#include <stdio.h>

int main(void) {
    int arr[5] = {10, 20, 30, 40, 50};

    printf("%d\n", arr[0]); // 10 -- first element
    printf("%d\n", arr[4]); // 50 -- last element (index = size - 1)

    return 0;
}
Accessing and Modifying Elements
You read or write an element using the subscript operator []. An array element behaves just like an ordinary variable of its type — it can appear on either side of an assignment, be passed to functions, or used in expressions.

C
#include <stdio.h>

int main(void) {
    int arr[5] = {1, 2, 3, 4, 5};

    arr[2] = 99;          // modify the element at index 2
    arr[0] = arr[0] + 10; // read and update using its own value

    for (int i = 0; i < 5; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }
    return 0;
}
Out-of-Bounds Access Is Undefined Behavior
C does not check array bounds
Unlike languages such as Java, Python, or C#, which throw an exception (e.g. IndexOutOfBoundsException) the moment you access an invalid index, C performs no bounds checking whatsoever. Writing arr[10] on a 5-element array compiles and may even appear to "work" sometimes — but it reads or writes memory that belongs to something else entirely. This is undefined behavior: it might silently corrupt another variable, crash the program, or appear to do nothing at all, depending on the run. Never rely on an out-of-bounds access failing loudly — it is your responsibility as the programmer to keep indices within 0 .. size - 1.

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

arr[5] = 100;  // UNDEFINED BEHAVIOR: index 5 is out of bounds (valid: 0-4)
arr[-1] = 100; // ALSO undefined behavior: negative indices are not checked either
Array Size Must Be a Compile-Time Constant
In classic (pre-C99) C, the size inside the square brackets of an array declaration must be a constant known at compile time — a literal number or a #define/const expression the compiler can evaluate before the program runs. You cannot write int arr[n]; where n is a variable whose value is only known while the program is executing.

C
#define SIZE 5

int arr1[5];    // OK: literal constant
int arr2[SIZE]; // OK: macro constant

int n = 5;
int arr3[n];    // NOT allowed in strict C89/C90 (n is a runtime value)
C99 relaxed this rule
Starting with the C99 standard, C introduced variable-length arrays (VLAs), which do allow the size to be a runtime expression like int arr[n];. VLAs come with their own tradeoffs and risks, covered in detail in the next page.
  • An array is a fixed-size, contiguous block of memory holding elements of the same type

  • Elements are accessed with zero-based indexing: valid indices run from 0 to size - 1

  • C performs no bounds checking — out-of-bounds access is undefined behavior, not a caught exception

  • In classic C, array size must be a compile-time constant (a literal or #define/const expression)

  • C99 introduced variable-length arrays (VLAs) to allow runtime-determined sizes