C Arrays

C Tutorial

📦 C Arrays (Complete Tutorial: Beginner → Advanced)

In C language, an array is a collection of multiple values of the same data type stored in contiguous memory locations.
Arrays are fundamental for DSA, system programming, embedded systems, and interviews.


1️⃣ What is an Array?

An array stores multiple values using a single variable name.

Example

int marks[5] = {70, 80, 90, 60, 85};

marks → array name
5 → number of elements
✔ Index starts from 0


2️⃣ Why Use Arrays?

✔ Store large data efficiently
✔ Easy data processing using loops
✔ Reduces code repetition
✔ Improves performance
✔ Basis of DSA (sorting, searching)


3️⃣ Array Declaration

Syntax

data_type array_name[size];

Examples



4️⃣ Array Initialization

Method_1: At Declaration

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

Method_2: Size Automatically Calculated

int a[] = {10, 20, 30};

Method_3: Partial Initialization

int a[5] = {1, 2}; // remaining = 0

5️⃣ Accessing Array Elements


 

📌 Index range: 0 to size-1


6️⃣ Array Traversal Using Loop ⭐


 

✔ Most common operation


7️⃣ Taking User Input in Array


 

✔ Used in exams & interviews


8️⃣ Types of Arrays

1️⃣ One-Dimensional Array

int a[5];

2️⃣ Two-Dimensional Array (2D)

int a[2][3];

3️⃣ Multidimensional Array

int a[2][3][4];

9️⃣ Arrays and Functions ⭐

Passing Array to Function


📌 Array is passed by reference


🔟 Size of an Array ⭐ (Important)

int n = sizeof(arr) / sizeof(arr[0]);

✔ Works only in the same scope
❌ Not inside function


1️⃣1️⃣ Arrays and Pointers 🔥


 

✔ Array name acts as pointer to first element


1️⃣2️⃣ Common Array Operations

Sum of Elements

sum += a[i];

Find Maximum

if (a[i] > max) max = a[i];

Reverse Array

for (i = n-1; i >= 0; i--)

1️⃣3️⃣ Limitations of Arrays ❌

❌ Fixed size
❌ Cannot grow dynamically
❌ Stores only same data type

✔ Solved using dynamic memory & structures


🔟4️⃣ Common Mistakes ❌

❌ Accessing out-of-bounds index
❌ Forgetting array size
❌ Using wrong loop condition
❌ Not passing size to function


📌 Interview Questions (Must Prepare)

  1. What is an array?

  2. Index range of array?

  3. Difference between array and variable?

  4. How to find array size?

  5. How arrays are passed to functions?

  6. Difference between array and pointer?


🔥 Real-Life Use Cases

  • Student marks list

  • Employee salary data

  • Sensor readings (embedded)

  • Game scores

  • Banking transactions


✅ Summary

✔ Arrays store multiple values efficiently
✔ Index starts from 0
✔ Traversal uses loops
✔ Passed by reference to functions
✔ Foundation for DSA, interviews & real projects

You may also like...