FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses C Programming From Zero

First year

Progress0 / 6 lessons
  1. 1. What a program actually is
  2. 2. Variables, types and exact sizes
  3. 3. Input and output with printf and scanf
  4. 4. Control flow and loops
  5. 5. Functions and how the call stack works
  6. 6. Arrays and strings in C

Courses › C Programming From Zero

Arrays and strings in C

Contiguous memory, the missing bounds check, and why a C string needs its null terminator.

12 min read · Lesson 6 of 6 · Pro

An array is one block of memory

C
#include <stdio.h>

int main(void)
{
    int marks[5] = {90, 75, 88, 60, 100};
    int i, total = 0;

    for (i = 0; i < 5; i++)
        total += marks[i];

    printf("Total %d, average %.2f\n", total, total / 5.0);
    return 0;
}

int marks[5] reserves 5 x 4 = 20 bytes, side by side, no gaps. marks[3] means "go to the start of the array, skip 3 x 4 = 12 bytes, read 4 bytes". That single multiplication is why array access is fast, and why the index starts at 0 — the first element needs no skipping.

Prove it to yourself:

C
#include <stdio.h>
The rest of this lesson is Pro

The free lessons of C Programming From Zero finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.