Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
Declare Arrays in C
Chapter 7, Lecture 2 of the free C programming course: the declaration syntax, how the compiler sizes your array, and why the size must be a constant in embedded code.
Before you can store a single sample, you must declare arrays in C: tell the compiler the element type, the name and the number of elements. Get the declaration right and everything else in this chapter follows naturally. This lecture belongs to our free C tutorial series and the free C programming course built for C programming for embedded systems, so we pay special attention to RAM cost and to the rules that embedded teams enforce on array sizes.
What You Will Learn
- The general syntax to declare arrays in C
- How element type and element count decide the memory footprint
- Why the classic rule says the size must be known at compile time
- How symbolic constants make array sizes easy to change
- What variable-length arrays are, and why firmware teams usually avoid them
- How to measure an array with
sizeofand compute its element count safely
Prerequisites
- Lecture 1 of this chapter (array vocabulary and memory layout)
- The
#definepreprocessor directive at a basic level - Knowing that
int,floatandcharoccupy different numbers of bytes
Syntax to Declare Arrays in C
A one-dimensional array declaration has exactly three parts, written in this order: the data type of every element, a name you choose, and the element count inside square brackets, ending with a semicolon.
data_type array_name[size];
| Part | Role | Here |
|---|---|---|
| int | Type of every element | Each element is an integer |
| adc_raw | Array name (any valid identifier) | The label you use in code |
| [ 8 ] | Element count | Eight elements, indices 0 to 7 |
The name must follow the normal identifier rules: letters, digits and underscore, not starting with a digit, and not a keyword. The size is a positive whole number, or a constant expression that evaluates to one, such as 4 * 2.
Three Declarations, Three Memory Footprints
Different element types give different footprints even when the counts are similar. Here are three declarations you might meet in one firmware project:
int adc_raw[8]; /* eight integers */
float volts[4]; /* four floats */
char tag[6]; /* six characters */
Rather than trusting a table in a book, ask your own compiler. This program prints the memory used by each array and the distance between neighbouring elements:
#include <stdio.h>
#define NUM_CHANNELS 8
int main(void)
{
int adc_raw[NUM_CHANNELS];
float volts[4];
char tag[6];
printf("adc_raw bytes : %zu\n", sizeof adc_raw);
printf("volts bytes : %zu\n", sizeof volts);
printf("tag bytes : %zu\n", sizeof tag);
printf("adc_raw count : %zu\n", sizeof adc_raw / sizeof adc_raw[0]);
printf("gap between adc_raw[0] and adc_raw[1]: %ld bytes\n",
(long)((char *)&adc_raw[1] - (char *)&adc_raw[0]));
printf("gap between volts[0] and volts[3] : %ld bytes\n",
(long)((char *)&volts[3] - (char *)&volts[0]));
return 0;
}
gcc -Wall -Wextra -std=c11 -o decl_sizes decl_sizes.c
./decl_sizes
Expected output where int and float are both four bytes:
adc_raw bytes : 32
volts bytes : 16
tag bytes : 6
adc_raw count : 8
gap between adc_raw[0] and adc_raw[1]: 4 bytes
gap between volts[0] and volts[3] : 12 bytes
Two things are worth noticing. First, the byte total is always count times element size: 8 times 4 is 32. Second, the expression sizeof adc_raw / sizeof adc_raw[0] recovers the element count without you typing the number again, which is the safest way to keep loops in step with the declaration. The pointer casts are only there to measure distances in bytes; you do not need them in everyday code, and pointers get their own chapter.
| Declaration | Count | Element size | Total bytes |
|---|---|---|---|
| int adc_raw[8] | 8 | 4 | 32 |
| float volts[4] | 4 | 4 | 16 |
| char tag[6] | 6 | 1 | 6 |
Why the Array Size Must Be Known Early
When the compiler meets a declaration, it must decide how much memory to set aside. For a global array that space is baked into the program image, and for a local array it is carved out of the stack when the function runs. In both cases the traditional C rule is the same: the size has to be a constant the compiler can evaluate while compiling. A plain variable does not qualify, because its value could change while the program runs.
Use a Symbolic Constant for the Size
The cleanest way to satisfy the rule is a named constant created with #define. The preprocessor replaces the name with its number before compilation, so the compiler sees an ordinary constant. If the hardware changes from 8 ADC channels to 12, you edit one line and every array and loop that uses the name follows.
#define NUM_CHANNELS 8
int adc_raw[NUM_CHANNELS]; /* valid: constant size */
float gain[NUM_CHANNELS]; /* same size, always in step */
Why one named constant wins
If the number 8 is typed into five declarations and six loops, changing it means eleven edits and one forgotten spot. A forgotten spot in an array loop is a memory-corruption bug. One #define turns eleven edits into one.
What About a Size Held in a Variable?
Consider this attempt:
int size = 15;
int marks[size]; /* size is a variable, not a constant */
The result depends on the language standard and on where the line sits:
| Situation | What happens |
|---|---|
| Global scope, any standard | Compile error: the array would be “variably modified at file scope” |
| Inside a function, C89/C90 mode | Error when building with -std=c89 -pedantic-errors |
| Inside a function, C99 and later | Accepted as a variable-length array (VLA); size is fixed at run time |
| Inside a function, C11 and later | VLAs are an optional feature, so a compiler may reject them |
So the old rule “never use a variable as the size” is the safe rule for portable and embedded code, even though C99 relaxed it for local arrays. Variable-length arrays take their memory from the stack at run time, and a size that is larger than expected can overflow a small stack with no warning. Many embedded coding standards, including MISRA C, forbid them. You can make GCC flag any accidental use with the -Wvla option.
Embedded rule of thumb: choose array sizes at design time, write them as named constants, and do the memory budget on paper. A firmware image that decides its own memory use at run time is hard to test and hard to certify.
Where the Declared Array Lives
| Declared where | Lifetime | Memory region on a typical MCU | Starting contents |
|---|---|---|---|
| Outside all functions | Whole program run | Static RAM (zero-initialised area unless you give values) | All zeros if no initialiser |
| Inside a function | Until the function returns | Stack | Garbage until you write it |
| Inside a function with static | Whole program run | Static RAM | All zeros if no initialiser |
This distinction matters in firmware. A 2 KB local array on a device with a 4 KB stack leaves almost nothing for the rest of the call chain, while the same array declared at file level costs permanent RAM but cannot crash the stack. Both choices are legitimate; you should simply make them on purpose. Whenever you declare arrays in C for a new firmware module, decide the memory region first and the size second, and write both decisions in a comment next to the declaration.
Common Mistakes When You Declare Arrays in C
- Putting the size in round brackets or braces instead of square brackets
- Using a variable as the size at file scope and then wondering about the compile error
- Declaring a size of zero or a negative number, which is not valid standard C
- Assuming a local array starts at zero; it starts with whatever was in that stack memory
- Typing the number of elements separately in the loop instead of deriving it from the declaration
Best Practices When You Declare Arrays in C
Good habits at the moment you declare arrays in C save hours of debugging later, because the declaration fixes the memory layout for the rest of the program. Keep this short list next to your keyboard while you work through the free C programming course.
- Create one
#define(or anenumconstant) per logical size and reuse it everywhere - Compute element counts with
sizeof array / sizeof array[0]in the same scope as the declaration - Keep big arrays at file scope, or
static, so they do not eat the stack - Compile with
-Wall -Wextra -Wvlaso surprises show up early - Write a comment with the memory cost when an array is large, for example “256 x 2 bytes = 512 bytes”
Summary and Key Takeaways
Key takeaways
- Declare arrays in C with: type, name, size in square brackets, semicolon
- Total bytes equal element count times element size
- The size should be a compile-time constant; use a named constant so it lives in one place
- Variable-length arrays exist in C99 but are optional in C11 and discouraged in embedded code
- Where you declare the array decides its lifetime, its memory region and its starting contents
Frequently Asked Questions
What is the syntax to declare an array in C?
Write the element type, the array name and the element count in square brackets, then end with a semicolon, for example int samples[10];.
Can I use a variable as an array size in C?
At file scope never. Inside a function, C99 allows it as a variable-length array, but C11 made that optional and embedded standards usually forbid it, so use a constant instead.
How do I find the number of elements in an array?
Divide the size of the whole array by the size of one element: sizeof arr / sizeof arr[0]. This only works in the scope where the array itself is declared.
Why use #define for the array size?
One named constant keeps the declaration and every loop in step. Changing the size later needs a single edit instead of many.
Does a local array start with zeros?
No. A local array holds unpredictable leftover values until you assign them. File-scope and static arrays start as zeros.
How much RAM does int arr[100] use?
400 bytes when int is four bytes wide. Always confirm with sizeof on your target toolchain.
Is there an array size limit in embedded C?
The limit is your available RAM (or flash for constant tables) and, for local arrays, the stack size. The language itself does not set a small cap.
Continue the Free Embedded C Course
Next: accessing array elements with subscripts, and the danger of going past the last index. This free C programming course teaches the habits that keep firmware safe.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments