🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Arrays in C Explained
Chapter 7, Lecture 1 of the free C programming course: why one variable name can hold hundreds of values, and why embedded firmware cannot live without it.
Every useful embedded program juggles groups of similar values: a burst of ADC samples, the bytes of a UART frame, a table of PWM duty cycles. Arrays in C are the tool that lets you keep all of them under a single name instead of inventing hundreds of separate variables. This lecture is part of our free C programming course and the free embedded C course track, so every idea is explained with embedded C programming in mind. If you were looking for a free C tutorial that starts from the very first array and ends in interview-ready answers, you are in the right place.
What You Will Learn
- The problem that arrays solve, and why separate variables stop scaling after about five values
- The precise meaning of array, element, index (subscript), size, lower bound and upper bound
- How the elements of an array sit side by side in memory
- The difference between one-dimensional and multi-dimensional arrays (the roadmap for this chapter)
- Where arrays show up in real firmware, so the syntax has a purpose
- How to compile and run your first array program and read its output
Prerequisites
- Variables and the basic data types
char,intandfloat - The
printf()function and conversion specifiers such as%d - The
forloop (Chapter 5) - A working compiler such as GCC on Linux, or an embedded toolchain that supports standard C
Arrays in C: Why You Need Them
Every variable you have declared so far holds exactly one value. That is fine for a loop counter or a single temperature. Now imagine a battery monitor that records the pack voltage once per second and must keep the last 100 readings so it can detect a slow drop. With plain variables you would have to:
- Declare one hundred names such as
v0,v1,v2and so on - Write one hundred assignment statements to fill them
- Write one hundred
printf()calls, or one huge expression, to use them
Nobody maintains code like that. Worse, you can never loop over the names, because a loop counter cannot be glued into an identifier. An array removes the problem: you declare one name, tell the compiler how many values it holds, and pick any individual value with a number.
The Formal Definition
An array is a fixed-size collection of values that all have the same data type, stored one after another in memory and reached through one shared name. Each value inside it is an element. The number that selects an element is its index, also called the subscript. The element type can be any valid type: char, int, float and, as you will see later in the course, structures and pointers too.
Indexing in C starts at zero. The first element is index 0, the second is index 1, and an array that holds 100 values ends at index 99. This surprises beginners for a week and then becomes second nature. The reason is practical: the index is really a distance from the start of the array, and the first element is zero steps away from the start.
How Arrays in C Sit in Memory
When you declare an array, the compiler reserves one unbroken block of memory big enough for every element. Element 0 sits at the lowest address and each following element sits directly after the previous one. Because every element has the same size, the address of any element can be computed with a single multiplication: start address plus index times element size. That is why reading samples[3] costs the CPU almost nothing, however large the array is.
| Element | adc[0] | adc[1] | adc[2] | adc[3] | adc[4] |
|---|---|---|---|---|---|
| Value | 512 | 498 | 530 | 505 | 521 |
| Address | 0x20000000 | 0x20000004 | 0x20000008 | 0x2000000C | 0x20000010 |
| Index | 0 (lower bound) | 1 | 2 | 3 | 4 (upper bound) |
Check your platform
The four-byte gap above assumes a 32-bit int, which is the norm on ARM Cortex-M and on desktop Linux. On a small 8-bit or 16-bit microcontroller an int may be only two bytes, so the gaps shrink. Never hard-code the element size; ask the compiler with sizeof.
Array Vocabulary at a Glance
| Term | Meaning | Example for int adc[5] |
|---|---|---|
| Element | One stored value | adc[2] holds 530 |
| Index / subscript | Number that selects an element | The 2 in adc[2] |
| Size | How many elements the array holds | 5 |
| Lower bound | First valid index, always 0 in C | 0 |
| Upper bound | Last valid index, which is size minus 1 | 4 |
| Dimension | Number of subscripts needed | 1 (one-dimensional) |
One-Dimensional and Multi-Dimensional Arrays
The number of subscripts an array needs is its dimension. A one-dimensional array uses one subscript and is often called a vector: a row of ADC samples, a line of text. A two-dimensional array uses two subscripts and is called a matrix: think of a small LED matrix or a table with rows for channels and columns for time. Higher dimensions follow the same pattern. This chapter first masters the one-dimensional case, because every other form is built from it, and then moves on to the multi-dimensional case.
Where Embedded Programs Use Arrays
| Firmware task | Typical array | Why an array fits |
|---|---|---|
| Averaging noisy sensor input | int samples[16] | Store a burst, then loop to sum it |
| Receiving serial data | char rx_buf[64] | Bytes arrive one by one and must stay in order |
| Generating a waveform | int sine_table[32] | Precomputed values replace slow maths |
| Sending a CAN frame | unsigned char data[8] | A classic frame carries up to 8 payload bytes |
| Reading a keypad | char keymap[16] | Table lookup from key number to character |
Notice the pattern: the data is homogeneous, the count is known in advance, and the order matters. Whenever you see those three properties, reach for an array.
Your First Arrays in C Program
The program below stores five ADC readings in an array and prints them with a loop. The braces after the declaration are an initialiser list that fills the elements at the moment of creation; we study initialisation in detail in a later lecture, so just read it as “put these five values in order”. The sizeof line shows how much memory the array occupies.
#include <stdio.h>
int main(void)
{
int adc_samples[5] = {512, 498, 530, 505, 521};
int i;
for (i = 0; i < 5; i++) {
printf("Sample %d = %d\n", i, adc_samples[i]);
}
printf("Total bytes used: %zu\n", sizeof(adc_samples));
return 0;
}
Build and run it with the commands below. The -Wall -Wextra flags turn on the compiler warnings you should always use in embedded work.
gcc -Wall -Wextra -std=c11 -o first_array first_array.c
./first_array
Expected output on a machine where int is four bytes:
Sample 0 = 512
Sample 1 = 498
Sample 2 = 530
Sample 3 = 505
Sample 4 = 521
Total bytes used: 20
Reading the Program Line by Line
int adc_samples[5]asks the compiler for five consecutive integers under the single nameadc_samples.- The loop counter
istarts at 0 and stops before 5, so it visits exactly the valid indices 0 to 4. adc_samples[i]uses the loop counter as the subscript, which is the core array idiom: one loop, one subscript, every element visited.sizeof(adc_samples)reports 20 because five elements times four bytes each equals twenty bytes.
Common Mistakes with Arrays in C
Off-by-one thinking. An array of size 5 has no element number 5. Its valid indices are 0 to 4. Counting from one is the most common beginner bug and the root of countless buffer overflows in real firmware.
- Declaring a huge array “just in case” on a microcontroller that has only a few kilobytes of RAM
- Mixing element types, for example trying to keep an integer and a text label in the same array
- Forgetting that the array name by itself is not a value you can assign to another array with
= - Using a magic number like 100 in five different places instead of one named constant
Best Practices from Day One
Habits formed in your first week with arrays in C tend to stay for a whole career, so it is worth starting them correctly.
- Choose a descriptive name:
adc_samplestells a reader more thana - Define the size once, with a named constant, and reuse it in every loop
- Estimate RAM use before declaring: elements times element size, summed over all arrays
- Compile with warnings on and treat warnings as errors in your own projects
Summary and Key Takeaways
Key takeaways
- An array stores many values of one type under one name
- Elements are selected by an index, and the first index is always 0
- The last valid index is the size minus 1
- Elements occupy consecutive memory, so access by index is fast
- One subscript means a one-dimensional array; two subscripts mean a matrix
- Embedded firmware uses arrays for sample buffers, lookup tables and communication frames
You now have the vocabulary and the mental picture of arrays in C, and you can already tell when a problem is begging for an array: many values, one type, a known count and an order that matters. Hold on to that test, because arrays in C appear in nearly every embedded project you will meet. The next lecture shows exactly how to declare an array, how the compiler decides its size, and why the size must be known before the program runs.
Frequently Asked Questions
What is an array in C in simple words?
It is a numbered row of boxes that all hold the same type of value. The whole row has one name, and each box is picked by its number.
Why does array indexing start from 0 in C?
The index measures how far an element is from the start of the array. The first element is zero steps away, so its index is zero. This also makes the address calculation a simple multiplication.
Can an array hold different data types?
No. Every element of one array has the same type. To group different types you use a structure, which is covered later in this free C programming course.
How much memory does an array use?
The number of elements multiplied by the size of one element. An int array of 5 elements uses 20 bytes when int is four bytes wide.
Why are arrays important in embedded C programming?
Firmware constantly handles groups of similar data such as sensor samples, serial buffers and lookup tables. Arrays store them compactly with predictable memory use, which matters on small devices.
What is the difference between a vector and a matrix?
A vector is a one-dimensional array with one subscript. A matrix is a two-dimensional array with two subscripts, arranged like rows and columns.
Is this free embedded C course suitable for beginners?
Yes. Each lecture starts from the basics, uses small runnable programs, and ends the chapter with a quiz and interview questions.
Keep Going with the Free C Programming Course
Next up: declaring arrays and understanding why the size must be a compile-time constant. Every lecture in this free embedded C course is built for students who learn by running code.

2 Comments