C Array Size

C Tutorial

📏 C Array Size (Beginner → Advanced)

Knowing the size of an array in C language is very important to avoid out-of-bounds errors, write generic code, and solve DSA & interview problems.


1️⃣ What Does “Array Size” Mean?

Array size can mean:

  1. Total number of elements

  2. Total memory occupied

  3. Size of one element


2️⃣ Finding Array Size Using sizeof() ⭐ (Most Important)

Example


 

✔ Works only inside the same scope where array is declared


3️⃣ Why sizeof(arr) Works?

Because:

  • arr is a real array

  • sizeof(arr) returns total memory of array

Example (on 4-byte int):

5 elements × 4 bytes = 20 bytes

4️⃣ Array Size in Function ❌ (Common Mistake)

📌 Inside a function, array becomes a pointer
📌 sizeof(arr) returns pointer size (4 or 8 bytes)


5️⃣ Correct Way: Pass Size as Parameter ⭐


 

✔ Best practice


6️⃣ Array Size for Different Data Types

TypeExampleSize per element*
intint a[5]4 bytes
floatfloat f[5]4 bytes
doubledouble d[5]8 bytes
charchar c[5]1 byte

*May vary by system


7️⃣ Size of Multidimensional Arrays ⭐


 

Number of Rows & Columns


8️⃣ Dynamic Array Size (Using malloc)

📌 Size must be stored separately
📌 sizeof(arr) gives pointer size ❌


9️⃣ String Size vs Length ⭐


 

✔ Very common interview question


🔟 Common Mistakes ❌

❌ Using sizeof on array inside function
❌ Confusing bytes with number of elements
❌ Hardcoding array size
❌ Accessing beyond array length


📌 Interview Questions (Must Prepare)

  1. How to find array length in C?

  2. Why sizeof(arr) fails inside function?

  3. Difference between sizeof and strlen?

  4. Size of 2D array calculation?

  5. How to handle size of dynamic arrays?


🔥 Real-Life Importance

  • Prevents buffer overflow

  • Safer loops

  • Generic functions

  • DSA correctness

  • Embedded & system programming


✅ Summary

✔ Use sizeof(arr)/sizeof(arr[0]) in same scope
✔ Pass size to functions
✔ Inside functions, array becomes pointer
sizeof returns bytes, not element count
✔ Critical for interviews & real projects

You may also like...