Video Coming Soon...

Created by Zed A. Shaw Updated 2026-11-26 20:48:52

07: Simple Arrays

A single integer (int) variable is useful, but what if you need to keep track of hundreds or thousands of them? You can't create one variable for every integer you need like this:

int var0 = 0;
int var1 = 0;
...
int var999 = 0;

You need a way to store these 1000 integers in one place, with a single name, and be able to access any integer from 0-999 at random. That's what an array does in C.

The Code

View Source file ex7.c Only

#include <stdio.h>

int main(int argc, char* argv[]) {
  int numbers[1000] = {123};

  printf("Number at 0 is %d\n", numbers[0]);

  printf("At 204 is %d\n", numbers[204]);

  numbers[204] = 8765;

  printf("At 204 it's now %d\n", numbers[204]);

  size_t x = 100;
  printf("at x %d\n", ++numbers[x]);

  // uncomment this to possibly make a crash
  // printf("At 20000 it's %d\n", numbers[20000]);
}

Break it Down

The breakdown of this code might require you to read Simple Array Operations next to fully understand, so read this once, then read the next section, then read this again.

int numbers[1000];
This creates an array of int that has 1000 elements. The = {123} initializes the first (index 0) element of the array to 123 but the rest of it will be 0. If you remove this = {123} then you should get an error. Try it.
printf("Number at 0 is %d\n", numbers[0]);
This prints out the int at index 0 (the first int). It should print 123.
printf("At 204 is %d\n", numbers[204]);
This prints out the int at index 204 (the 205th int). It should print 0 since you didn't set it to anything.
numbers[204] = 8765;
This sets the int at index 204 (the 205th int) to 8765.
printf("At 204 it's now %d\n", numbers[204]);
Now when you print it you can see it's 8765.

The key point to understand with this code is that name is the name of the array, and [10] is how you access the element at index 10 (the 11th element). The best way to grasp this is to play with this code and access different elements.

Array Indexes Start at 0

Like with many languages the index to an array starts at 0. This can be confusing for new people, so there's an easy way to "translate" from what you use to what a computer wants.

  1. Any time you add a "th", "rd" or "nd" to a number you are using "ordinal numbers." They are human numbers that show an order to things like "1st", "2nd", "3rd" for "first", "second", "third". We use these numbers when talking about things that have an order like dates and who won races. Nobody ever say, "My horse came in 0th in the race!"
  2. If you say an ordinal number, subtract one and that's the index. So you want the 1st number? That's index 0 (1 - 1 == 0). This turns it into a cardinal number, used for indexing things and addresses. Or, "I went to house #340 on Lane Street."
  3. To convert from an cardinal number to ordinal add one. "House #340 is the 341st house."

The way to remember the difference is an "ordinal" number means it's talking about ordered things. Those have "st", "rd", and "nd" as in "1st", "2nd", "3rd". Cardinal numbers are the, "Other numbers that start at 0 and are -1 from the ordinal number." You only have to say what you want, "The first element," see it has "st", realize it's ordinal, and subtract 1 to get 0.

Overflow the Array

At the end of this code is this line:

  // printf("At 20000 it's %d\n", numbers[20000]);

If you remove the comment and recompile you might get a crash like this:

$ ./ex7
Number at 0 is 123
At 204 is 0
At 204 it's now 8765
Segmentation fault         (core dumped) ./ex7

This is on Linux, so on Windows or macOS you might get something different, or nothing at all. Keep increasing the 20000 until it crashes.

The reason this happens is because you've declared the array numbers to have 1000 elements, but numbers[20000] is way past the end. You've effectively driven your program off a cliff and into an ocean. The OS and your CPU will react to this by crashing, but C does almost nothing to protect you from this. In some cases you'll get a crash, in others it'd print garbage, and it might even do other odd things depending on the system.

We'll get more into this in later exercises, but just remember that if you ever see this Segmentation fault then it's probably because you accessed computer memory that your program doesn't own like this.

Simple Array Operations

I'll cover arrays multiple times in the course, but here's a few simple operations to try on your own:

numbers[x]
You can use another int to index. Try unsigned int and size_t for x.
numbers[10] = y
You can assign a variable to an index.
numbers[x] = y
You can combine the two, using one variable x as the index, and another y as the value to assign.
++numbers[x]
You can use numbers[x] in most places you'd use a regular variable like y. This would increment the number there.
int j = numbers[x] - numbers[y]
Since you can use numbers[x] anywhere another variable would go you can use them in math expressions like this too.

In programming you should always be able to combine most concepts with other concepts. When you learn something new, try to combine it with something else you know to discover what you can do.

Imposture Check

  1. Try all the different operations in Simple Array Operations.
  2. Rewrite this code to use float, double, and unsigned int.
  3. What happens if you try assigning float to an array of int or other combinations of types.
  4. Use the atoi function to assign strings that are numbers to your array of integers.
  5. Try to break this as much as you can. It's early but see what you can get do.
  6. The char *argv[] is an array of strings that represent your command line arguments. Do something with that knowledge.
Previous Lesson Next Lesson

Register for From Impostor to Programmer

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.