C Programming - Beginner to Brilliance / Arrays and Strings

Introduction to Array

🧠 What Is an Array?

Imagine you are a teacher with 100 students. You want to store each student’s marks.

If you use separate variables:

int mark1, mark2, mark3, ..., mark100;

😩 That’s exhausting, error-prone, and not practical.

✅ Instead, an array lets you store all 100 marks in a single variable name, like:

int marks[100];

🔁 So, an array is like a row of lockers 📦 — each box (element) holds a value and has a unique number (index).


📚 Simple Definition

An array in C is a collection of fixed-size elements of the same data type, stored in continuous memory locations, and accessed using an index.


🗃️ Real-Life Analogy: “Mailboxes”

📬 Suppose your apartment has 5 mailboxes:

  • Each mailbox is labeled 0 to 4 (just like array indexes).

  • All mailboxes store letters (same type).

  • You can access a specific one using its number (index).

Same way:

int boxes[5];  // 5 boxes, for integer letters

📌 Key Points About Arrays

🔎 Point

✅ Description

🎯 Fixed Size

Once declared, size can't change

🧱 Same Type

All elements must be of same type (e.g., int, char)

🧮 Indexed

Index starts from 0

🔗 Continuous

Stored in continuous memory blocks

⛔ No Resize

C arrays don't grow/shrink dynamically


🛠️ Syntax of Array Declaration

data_type array_name[size];

💡 Examples:

int numbers[5];       // Integer array of size 5
float prices[10];     // Float array of 10 elements
char letters[26];     // Character array for A-Z

🧾 How Memory Works Internally

Let’s say:

int arr[3] = {10, 20, 30};

This means:

Index

Value

Memory Address (Example)

0

10

1000

1

20

1004

2

30

1008

(Assuming each int is 4 bytes)

✅ Arrays are stored back-to-back in memory, which makes accessing elements very fast.


🧩 Why Use Arrays?

Without Array:

int a = 5, b = 10, c = 15;

With Array:

int numbers[3] = {5, 10, 15};

Now you can do:

for(int i = 0; i < 3; i++) {
   printf("%d ", numbers[i]);
}

✔️ Less Code

✔️ Loop-Friendly

✔️ Scalable to 1000+ elements

✔️ Easy to pass to functions


🔄 Accessing Array Elements

You can access or modify array values using index:

numbers[0] = 25;      // Changes first element
printf("%d", numbers[2]);  // Prints third element

⚠️ Array indexes start from 0. So arr[5] in an array of size 5 is invalid!


🔍 Default Values in Arrays

  • If you don’t initialize:

int x[3];  // x may contain garbage values
  • If you partially initialize:

int x[5] = {1, 2};  // Rest become 0 → {1, 2, 0, 0, 0}

🧪 Example: Student Marks

#include <stdio.h>

int main() {
    int marks[5];  // stores marks of 5 students

    for(int i = 0; i < 5; i++) {
        printf("Enter mark %d: ", i + 1);
        scanf("%d", &marks[i]);
    }

    printf("Entered marks:\\n");
    for(int i = 0; i < 5; i++) {
        printf("marks[%d] = %d\\n", i, marks[i]);
    }

    return 0;
}
Enter mark 1: 80
Enter mark 2: 60
Enter mark 3: 40
Enter mark 4: 95
Enter mark 5: 75
Entered marks:
marks[0] = 80
marks[1] = 60
marks[2] = 40
marks[3] = 95
marks[4] = 75

❌ Common Mistakes

Mistake

Explanation

Accessing out of bounds like arr[5] in arr[5]

Causes undefined behavior

Using different data types

Not allowed

Forgetting to initialize

May hold garbage


💡 Array vs Variable Comparison

Feature

Variable

Array

Storage

One value

Many values

Access

Direct

By index

Memory

One block

Continuous blocks

Syntax

int x = 10;

int arr[5];


How to check the length of arrays in C??

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);
    printf("Length of array = %d\\n", length);
    return 0;
}
Length of array = 5