Arrays
8 min · C
Fixed-size arrays: hold many values under one name.
Enemy 1 of 5
Declaring and indexing an array
This code makes an array of three ints named nums and prints the first one, using index 0. Indexes start at zero. Press Run to see it.
Why this works
An array groups several values of the same type together under one name, stored contiguously in memory (right next to each other). You declare one with type name[size]; (for example int nums[3]; makes room for exactly three ints) and you can provide initial values right away with curly braces, like int nums[3] = {3, 1, 2};.
Elements are accessed with square brackets and a zero-based index: nums[0] is the first element, nums[1] is the second, nums[2] is the third (and last, in a 3-element array). Reading or writing nums[3] on this array would reach one slot past the end. C does not stop you from doing this, and it's one of the most notorious sources of bugs and security vulnerabilities in real C programs, so always double-check your bounds.
Rena
Your AI tutor. She walks you through each step.
Asking for help is part of coding. Rena reads your code and gives you a hint. She is free with an account: 20 hints a day.