Question

How to Determine Array Size in C

carrays

Question

In C, how can I determine the size of an array, meaning the number of elements it can hold?

For example, if I declare an array, how do I calculate how many items fit in it rather than just its memory size in bytes?

Short Answer

By the end of this page, you will understand how to get the number of elements in a C array using sizeof, why this works only in certain contexts, and what changes when arrays are passed to functions.

Concept

In C, an array stores multiple values of the same type in contiguous memory. Two related ideas are important:

  • Total array size in bytes
  • Number of elements in the array

C does not provide a built-in .length property like some other languages. Instead, for a real array declared in the same scope, you usually calculate its element count like this:

sizeof(array) / sizeof(array[0])

Why this works:

  • sizeof(array) gives the total number of bytes used by the entire array.
  • sizeof(array[0]) gives the number of bytes used by one element.
  • Dividing them gives the number of elements.

Example:

int numbers[10];

If int is 4 bytes on your system:

  • sizeof(numbers) is 40
  • sizeof(numbers[0]) is 4
  • 40 / 4 = 10

This matters because C gives you low-level memory control, but that also means you must often track array sizes yourself. Understanding this pattern is essential for loops, validation, copying data, and avoiding out-of-bounds access.

A very important limitation: this only works when the variable is truly an array in that scope. If you pass the array to a function, it usually becomes a pointer, and sizeof will no longer give the element count.

Mental Model

Think of an array like a row of identical boxes in a warehouse:

  • The whole shelf width is sizeof(array).
  • The width of one box is sizeof(array[0]).
  • If you divide shelf width by box width, you get how many boxes fit on the shelf.
total shelf width / one box width = number of boxes

This works only when you are looking at the actual shelf. If someone gives you just a note pointing to the first box, you no longer know how long the whole shelf is. That is what happens when an array is passed to a function in C: it decays into a pointer.

Take Quiz

Syntax and Examples

The standard pattern is:

sizeof(array) / sizeof(array[0])

Example 1: Integer array

#include <stdio.h>

int main(void) {
    int numbers[] = {10, 20, 30, 40, 50};
    size_t count = sizeof(numbers) / sizeof(numbers[0]);

    printf("Number of elements: %zu\n", count);
    return 0;
}

Output:

Number of elements: 5

Explanation:

  • sizeof(numbers) returns the total bytes of the whole array.
  • sizeof(numbers[0]) returns the bytes of one int.
  • Dividing gives the element count.

Example 2: Character array

#include <stdio.h>

int main(void) {
    char letters[] = {'a', 'b', 'c', 'd'};
    size_t count = sizeof(letters) / sizeof(letters[0]);

    printf("Number of elements: %zu\n", count);
    return 0;
}

Example 3: Macro for convenience

#include <stdio.h>

#define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))

int main(void) {
    double prices[] = {2.5, 4.0, 6.75};
    printf("Array length: %zu\n", ARRAY_LEN(prices));
    return 0;
}

This macro is convenient, but only safe when arr is an actual array, not a pointer.

Step by Step Execution

Consider this program:

#include <stdio.h>

int main(void) {
    int values[] = {5, 10, 15, 20};
    size_t count = sizeof(values) / sizeof(values[0]);

    printf("%zu\n", count);
    return 0;
}

Step by step:

  1. int values[] = {5, 10, 15, 20};

    • C creates an array with 4 elements.
    • Each element is an int.
  2. sizeof(values)

    • This asks for the total size of the whole array in bytes.
    • If int is 4 bytes, then the result is 16.
  3. sizeof(values[0])

    • This asks for the size of one element.
    • If int is 4 bytes, then the result is 4.
  4. 16 / 4

    • The result is 4.
  5. printf("%zu\n", count);

    • Prints 4.

Important function example

#include <stdio.h>

void print_length(int arr[]) {
    size_t count = sizeof(arr) / sizeof(arr[0]);
    printf("Inside function: %zu\n", count);
}

int main(void) {
    int values[] = {5, 10, 15, 20};
    size_t count = sizeof(values) / sizeof(values[0]);

    printf("In main: %zu\n", count);
    print_length(values);
    return 0;
}

What happens:

  • In main, values is a real array, so the calculation works.
  • In print_length, arr is treated like a pointer to the first element.
  • sizeof(arr) gives the size of the pointer, not the whole array.
  • So the calculation inside the function is wrong.

That is why array length is often passed separately to functions.

Real World Use Cases

Knowing array size is useful in many practical situations:

Looping through fixed arrays

for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
    printf("%d\n", values[i]);
}

Validating buffer limits

If you have a fixed-size array, you need its length to avoid writing past the end.

Processing lookup tables

Programs often use arrays for:

  • menu options
  • sensor calibration values
  • status code tables
  • test data sets

Embedded systems

In embedded C, fixed-size arrays are common because memory is limited and controlled carefully.

String-like character buffers

Even though strings in C use a null terminator, character arrays still have a fixed capacity that matters for safe input and output.

Take Quiz

Real Codebase Usage

In real C projects, developers commonly use array sizes in a few predictable ways.

Pass length into functions

void print_numbers(const int arr[], size_t length) {
    for (size_t i = 0; i < length; i++) {
        printf("%d\n", arr[i]);
    }
}

This is the standard pattern because the function cannot reliably determine the original array length by itself.

Use helper macros

#define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))

This is often used for local static arrays.

Guard against out-of-bounds access

if (index < length) {
    printf("%d\n", arr[index]);
}

Configuration tables

Arrays are often used to store fixed settings or mappings:

const char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};
size_t day_count = sizeof(days) / sizeof(days[0]);

Validation and iteration

Many codebases compute array length once and reuse it instead of recalculating repeatedly:

size_t count = sizeof(items) / sizeof(items[0]);
for (size_t i = 0; i < count; i++) {
    /* process items[i] */
}

Common Mistakes

1. Using sizeof on an array parameter

Broken example:

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

Problem:

  • arr is not the full array here.
  • It behaves like int *arr.
  • sizeof(arr) is the size of a pointer.

Fix:

void f(int arr[], size_t n) {
    printf("%zu\n", n);
}

2. Confusing bytes with element count

Broken example:

int nums[10];
printf("%zu\n", sizeof(nums));

Problem:

  • This prints bytes, not the number of elements.

Fix:

printf("%zu\n", sizeof(nums) / sizeof(nums[0]));

3. Hard-coding the length

Broken example:

int nums[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 10; i++) {
    printf("%d\n", nums[i]);
}

Problem:

  • The loop goes past the array bounds.

Fix:

size_t count = sizeof(nums) / sizeof(nums[0]);
for (size_t i = 0; i < count; i++) {
    printf("%d\n", nums[i]);
}

4. Using the wrong printf format specifier

Broken example:

size_t count = sizeof(nums) / sizeof(nums[0]);
printf("%d\n", count);

Problem:

  • count is size_t, not int.

Fix:

printf("%zu\n", count);

5. Expecting dynamically allocated memory to know its length

Broken idea:

int *nums = malloc(10 * sizeof(int));
size_t count = sizeof(nums) / sizeof(nums[0]);

Problem:

  • nums is a pointer.
  • sizeof(nums) gives pointer size, not allocated element count.

Fix:

  • Store the length in a separate variable.
size_t count = 10;
int *nums = malloc(count * sizeof(int));

Comparisons

ConceptWhat it gives youWorks for real arrays?Works for pointers?
sizeof(array)Total bytes of the full arrayYesNo
sizeof(array[0])Bytes of one elementYesYes
sizeof(array) / sizeof(array[0])Number of elementsYesNo
Stored length variableNumber of elementsYesYes

Array vs pointer

FeatureArrayPointer
Represents fixed collection in declarationYesNo
Knows total byte size with sizeof in same scopeYesNo
Passed to function without decayNoAlready a pointer
Needs separate length tracking in many APIsOftenYes

Fixed array vs dynamic allocation

SituationBest approach
int a[5];Use sizeof(a) / sizeof(a[0]) in the same scope
int *a = malloc(...);Store length separately
Function parameterPass array and length together
Take Quiz

Cheat Sheet

sizeof(array) / sizeof(array[0])

Rules

  • Use this only when array is an actual array in the current scope.
  • sizeof(array) returns total bytes.
  • sizeof(array[0]) returns bytes per element.
  • The result type is usually size_t.
  • Print size_t with %zu.

Good example

int nums[] = {1, 2, 3, 4};
size_t len = sizeof(nums) / sizeof(nums[0]);

Function pattern

void process(const int arr[], size_t len) {
    for (size_t i = 0; i < len; i++) {
        /* use arr[i] */
    }
}

Macro

#define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))

Watch out

  • Does not work for pointers
  • Does not work for malloc memory
  • Does not work correctly on array parameters inside functions

FAQ

How do I get the length of an array in C?

Use:

sizeof(array) / sizeof(array[0])

This gives the number of elements, not the byte size.

Why doesn't C have a built-in array length property?

C is a low-level language. Arrays are closely tied to memory layout, and the language does not attach a runtime length property to them.

Does sizeof return the number of elements?

No. sizeof returns a size in bytes. You divide by the size of one element to get the element count.

Why does this stop working in functions?

Because array parameters are treated as pointers. A pointer does not know how many elements are in the original array.

Can I use this with dynamically allocated arrays?

No. For memory allocated with malloc, you must store the length yourself.

What type should I use for array length in C?

Use size_t, because it is the standard type for sizes and counts related to memory.

Is sizeof(array) / sizeof(*array) also valid?

Yes. This is equivalent and often used:

sizeof(array) / sizeof(*array)

It can be a nice shorthand.

Take Quiz

Mini Project

Description

Build a small C program that stores a fixed list of temperatures in an array, calculates how many readings exist, and prints each value. This demonstrates the most common real use of array length: safe iteration without hard-coding the number of elements.

Goal

Create a program that calculates an array's element count with sizeof and uses that count to loop through the array safely.

Requirements

  • Declare a fixed-size array of integers with sample temperature values.
  • Compute the number of elements using sizeof(array) / sizeof(array[0]).
  • Print the total number of readings.
  • Use a loop to print each reading.
  • Use size_t for the count and loop index where appropriate.
Take Quiz

Keep learning

Related questions

View more