Powerful Ways to Find the Size of an Array in C with the size of Operator

โญ Key Highlights

  • C programming language
    C programming language

๐Ÿง  What Is Actually Happening Under the Covers When Working in the C Programming Language?

You will swiftly learn something important: arrays are not magic boxes. They are only blocks of contiguous memory. That is all.

And as much as we would like C to say

“Hey buddy, you have an array with 5 items.”

It does not.

An array in the C programming language only stores the values; nothing else.

However, C does have one wonderful tool: sizeof
It lets us know how large something is in memory.

That is the cracked door that we need.

๐Ÿงฉ How size of Can Be Used to Find the Size of an Array

This is the trick I learned in college (after not realizing it for two weeks ๐Ÿ™ƒ):

Formula:

Total size of array in bytes / size of one element = number of elements

Let me break this down with the example that finally made sense to me:

int numbers[] = {10, 20, 30, 40, 50};

int totalBytes = sizeof(numbers);
int singleElement = sizeof(numbers[0]);
int length = totalBytes / singleElement;

The Output:

5

In the C programming language, this trick works nicely for you, as long as the array has not decayed to its pointer.

๐Ÿงญ Subheading Using Focus Keywords

How to Use size of With An Array in the C Programming Language (With Examples)

Hereโ€™s the method I use โ€” and honestly, I still whisper the formula to myself like a mantra:

Example 1: Integer Array

int scores[] = {5, 10, 15, 20};

int size = sizeof(scores) / sizeof(scores[0]);
printf("Array Size: %d", size);

This gives you 4, because there are four integers.

Example 2: Char Array

char letters[] = {'A', 'B', 'C'};
printf("%lu\n", sizeof(letters) / sizeof(letters[0]));

Result?
๐Ÿ‘‰ 3


๐Ÿ‘€ But Wait โ€” Hereโ€™s Where Most Beginners Break Their Code

When coding in the C programming language, itโ€™s easy to do this mistake:

void printSize(int arr[]) {
    printf("%lu", sizeof(arr)); 
}

You might expect to get the full array size.
NOPE. โŒ

When you pass An Array in the C Programming Language to a function, it โ€œdecaysโ€ into a pointer.

So inside the function, arr is actually:
๐Ÿ‘‰ int*

And the size of a pointer is typically 8 bytes on 64-bit systems.

Iโ€™ve personally had bugs caused by this โ€” the kind that make you question your life choices.


Soโ€ฆ How Do We Handle It Correctly?

Two safe ways:

1. Pass the Size as an Argument

void printSize(int arr[], int size) {
    printf("%d", size);
}

2. Use Macros (Popular in Real Projects)

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

Then:

int marks[] = {12, 24, 36, 48};
printf("%d", ARRAY_SIZE(marks));

I personally like macros. More readable. Less thinking. โœ”๏ธ


๐Ÿ” Real-Life Example From My Experience

When coding in the C programming language back in my first internship, I wrote a function to compute averages:

float avg(int arr[]) {
    int size = sizeof(arr) / sizeof(arr[0]); // Wrong!
}

And guess what โ€” the size kept returning 2, which was the size of a pointer on that machine.

I spent 5 hours debugging what could have been fixed with one Google search.
So trust me when I say:

๐Ÿ‘‰ Never use sizeof on an array inside a function.



๐Ÿ”ข More Practical Examples (Because Real Learning Needs Them)

Example: Float Array

Float Array
Float Array
float prices[] = {9.99, 19.99, 29.99};
int count = sizeof(prices) / sizeof(prices[0]);
printf("%d", count);

Result ๐Ÿ‘‰ 3


Example: 2D Array

Hereโ€™s something cool โ€” sizeof works differently with multi-dimensional arrays.

int matrix[3][4];

int total = sizeof(matrix) / sizeof(matrix[0]);
printf("%d", total);

This returns 3 โ€” the number of rows.

But:

int columns = sizeof(matrix[0]) / sizeof(matrix[0][0]);

Result ๐Ÿ‘‰ 4

When coding in the C programming language, this 2D trick is one of the most useful patterns youโ€™ll learn.


๐Ÿงจ Common Pitfalls Iโ€™ve Seen (and Made)

  • โŒ Using sizeof on a pointer

  • โŒ Using sizeof inside a function

  • โŒ Forgetting that arrays are fixed-size

  • โŒ Assuming C tracks length for you (it doesnโ€™t)

  • โŒ Using strlen on non-string arrays

Avoid these and youโ€™ll instantly level up your C skills.


๐ŸŽฏ Conclusion

When coding in the C programming language, knowing how to get the size of an array using the sizeof operator is one of those foundational skills that will save you endless headaches.

Whenever you work with An Array in the C Programming Language, remember:

Array in the C Programming Language
Array in the C Programming Language

โœจ sizeof(arr) / sizeof(arr[0]) is your friend.
โœจ Donโ€™t use it inside a function.
โœจ C doesnโ€™t store array sizes โ€” you must manage them.
โœจ Understanding memory = writing reliable programs.

Previous Article

Top 10 Python Collections in 2025 You Must Master to Level Up Your Code ๐Ÿš€

Next Article

LLM Full Form Explained: The 2025 Power Guide to Large Language Models Transforming AI Forever ๐Ÿš€

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam โœจ