What is an Array and Why Do We Need One?
Suppose your program needs to work with 20 integers — reading them, processing them, and printing them. One approach is to declare 20 separate variables, each with its own name. That works for a small number, but it quickly becomes unmanageable. Imagine doing the same thing for 200, 2000, or 20,000 values. You'd need a separate variable declaration and a separate line of code for every single one of them.
Arrays solve this problem. Instead of giving each value its own name, an array groups many values of the same data type under a single name, and lets you refer to individual values by their position (called an index or subscript).
Characteristics of an Array
An array has three defining properties:
- Fixed size — the number of elements is set at the time the array is declared and does not change during the program's execution.
- Same data type — every element in the array must be of the same type (all integers, all floats, all characters, etc.).
- Contiguous storage — the elements are stored one after another in memory, which makes it possible to calculate the address of any element efficiently.
Because elements are stored in sequence, you can refer to them by position: the first element, the second element, and so on. In C, positions are counted starting from zero. So for an array named num with 20 elements, the valid positions are num[0] through num[19].
The notation num[i] — a name followed by an integer expression in square brackets — is called index notation. The value inside the brackets is the index (or subscript), and it tells the compiler which element you want. Using a variable like i as the index is what makes it possible to process entire arrays with loops.
Declaring an Array in C
Before an array can be used it must be declared. The declaration tells the compiler three things: the name of the array, the data type of its elements, and how many elements it holds.
General form for a one-dimensional array:
data_type array_name[size];
data_type— the type of each element (int,float,char,double, etc.)array_name— any valid C identifiersize— a positive integer constant specifying the number of elements
Examples:
int scores[20]; /* 20-element integer array */
char labels[30]; /* 30-element character array */
float data[20]; /* 20-element floating-point array */
double values[12]; /* 12-element double array */
The storage class is optional and can be placed before the data type. If omitted, the default depends on where the array is declared (automatic inside a function, external outside).
How C Finds an Element in Memory
The array name itself is a symbolic reference to the address of the first element in memory. To locate any specific element, C uses this formula:
element address = base address + (size of one element × index)
For example, if an integer array starts at memory address 10,000 and each integer occupies 2 bytes, the element at index 3 is at:
10,000 + (2 × 3) = 10,006
This is why array indexing is fast — calculating an address from a base and an offset is a single arithmetic operation.
Storing Values in an Array
Declaring an array only reserves memory — it does not put any values in. There are three ways to get values into an array.
1. Initialization at Declaration
You can supply initial values in curly braces at the point of declaration:
/* General form */
data_type array_name[size] = {value1, value2, ..., valueN};
Rules:
- You cannot supply more values than there are elements — that is a compile error.
- If you supply fewer values than the declared size, the remaining elements are automatically set to zero.
- If you initialize all elements, you can omit the size and the compiler will count them for you.
Examples:
int a[5] = {5, 3, 2, 7, 9}; /* all 5 elements initialized */
int b[] = {11, 21, 75, 24, 5}; /* size inferred as 5 */
int c[15] = {3, 7, 4, 6, 1}; /* first 5 set; elements 5–14 = 0 */
2. Reading Values from the Keyboard
Use a loop combined with scanf() to fill an array at runtime:
int scores[10];
int i;
for (i = 0; i < 10; i++) {
scanf("%d", &scores[i]);
}
Note that &scores[i] uses the address-of operator — required by scanf() just as it is for ordinary variables.
3. Assigning Values Individually
You can assign a value to any single element using the assignment operator:
scores[4] = 23;
You cannot assign one array directly to another (e.g., second = first; is illegal). To copy an array you must loop through it element by element:
for (i = 0; i < 25; i++)
second[i] = first[i];
Character Arrays and Strings
A string in C is stored as a character array. The key difference from other arrays is the null terminator: every string ends with the special character '\0', which the compiler adds automatically when you initialize with a string literal.
char city[6] = "HELLO";
This creates a 6-element array: 'H', 'E', 'L', 'L', 'O', '\0'. You can also let the compiler determine the size:
char city[] = "HELLO"; /* compiler sets size to 6 automatically */
If you declare the array without an initial value and enter the size yourself, make sure to add one extra slot for the null terminator.
Multidimensional Arrays
C supports arrays with more than one dimension. A two-dimensional array is the most common and can be thought of as a table with rows and columns.
Declaration:
data_type array_name[rows][columns];
Examples:
float grid[5][3]; /* 5 rows, 3 columns of floats */
int table[3][3]; /* 3×3 integer table */
Initialization of 2D Arrays
Values are assigned row by row (the last subscript increases fastest):
int mat[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
This produces:
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
You can also use nested braces to make the row grouping explicit:
int mat[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
If a row's inner brace contains fewer values than the column count, the remaining elements in that row are set to zero. However, you cannot put more values in a row's inner brace than the declared column count — that is a compile error.