HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Arrays

Arrays

Fixed-size arrays, index access, and looping through them.

Simple enough for a 5-year-old to understand

Create an array and access elements

Example code
1
#include <stdio.h>
2
int main() {
3
int numbers[3] = {10, 20, 30};
An array with 3 elements.
DataAn array with 3 elements.
4
printf("%d\n", numbers[0]);
First element: 10
Output / InputFirst element: 10
5
printf("%d\n", numbers[2]);
Third: 30
Output / InputThird: 30
6
return 0;
7
}
DataOutput / Input
💡 Hint: C does not perform bounds checking: if you write numbers[5], the program may crash or read garbage values.

About this topic

In C, an array is a block of values of the same type stored side by side in memory. Being adjacent is the core concept: knowing the address of the first element, the compiler calculates the addresses of other elements with a simple multiplication. That is why array access is extremely fast.

The array size is fixed at the moment it is defined and cannot be resized later. If you want a structure whose size can change at runtime, you must allocate memory manually and resize it when necessary; C does not do this for you.

There is NO bounds checking in C arrays. Reading the tenth element of a five-element array does not throw an error; it just fetches whatever is at that address. If you perform a write operation, you overwrite another variable, and the error shows up much later in an unrelated place.

What you will learn

  • Defining an array and that indices start from zero
  • Calculating the array size using sizeof
  • Why out-of-bounds access works silently
  • That the array is passed to the function as a pointer

When to use?

If the number of elements is known in advance and will not change, an array is the most efficient choice: it consumes no extra memory and access is constant-time.

If the count is uncertain, either set an upper limit and use a fixed array or manage memory manually. The second approach is more flexible, but you must remember to free the allocated memory.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    int numbers[3] = {10, 20, 30};
    for (int i = 0; i <= 3; i++) {
        printf("%d\n", numbers[i]);
    }
    return 0;
}

The indices of a three-element array are zero, one, and two; because the loop condition uses less than or equal to, it accesses the index three as well. Since C does not perform bounds checking, it does not throw an error, it prints garbage data outside the array. The loop condition must always be strictly less than the number of elements.

Correct

#include <stdio.h>

int main() {
    int numbers[3] = {10, 20, 30};
    for (int i = 0; i < 3; i++) {
        printf("%d\n", numbers[i]);
    }
    return 0;
}

Wrong

#include <stdio.h>

void show(int arr[]) {
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", n);
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    show(numbers);
    return 0;
}

When an array is passed to a function, it decays into a pointer and size information is lost. Inside, sizeof gives the size of the pointer, not the array; the result is usually one, not five. It is mandatory to pass the number of elements as a second parameter.

Correct

#include <stdio.h>

void show(int arr[], int n) {
    printf("%d\n", n);
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    show(numbers, 5);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    int a[3] = {1, 2, 3};
    int b[3];
    b = a;
    printf("%d\n", b[0]);
    return 0;
}

Arrays cannot be copied using assignment; the compiler throws an "assignment to expression with array type" error. The array name behaves like an address, not a value, and cannot be assigned. To copy it, you must iterate through the elements one by one or use memcpy.

Correct

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

int main() {
    int a[3] = {1, 2, 3};
    int b[3];
    memcpy(b, a, sizeof(a));
    printf("%d\n", b[0]);
    return 0;
}

Frequently asked questions

Can I determine the array size at runtime?

Since C99, you can use a variable for the size, but this array lives on the stack and large sizes will crash the program. Also, it cannot be resized later. For truly dynamic sizing, you need to allocate memory with malloc and release it with free when you are done.

How do I find out the number of elements in an array?

Inside the function where the array is defined, you find it by dividing the total size of the array by the size of a single element. This calculation only works there; once the array is passed to a function, the size information is lost, so the same calculation yields an incorrect result.

Will my program crash immediately if I write out of bounds?

Usually no, and that is the real problem. A few bytes of overflow corrupt a neighboring variable, the program keeps running, and the error surfaces somewhere completely different. That is why you should keep compiler warnings enabled and use memory checking tools.

Summary

An array is a fixed-size block of elements of the same type stored adjacently in memory.

Indices start from zero, and the last index is one less than the number of elements.

C does not perform bounds checking; overflow is not an error, it is silent memory corruption.

When an array is passed to a function, its size information is lost and it must be passed separately.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code