Arrays
What is an array in C?
An array is one of the most important concepts in C programming. It allows us to store multiple values of the same data type using a single variable name.
For example, instead of declaring five separate variables to store five students' marks, we can use a single array.
Definition of an array
An array is a collection of elements of the same data type stored in contiguous memory locations and accessed using a common name and an index.Example:
int marks[5] = {85, 90, 75, 88, 95};
Advantages of arrays
- Store many values under one variable name.
- Access elements directly using their indexes.
- Reduce repetitive variable declarations.
- Simplify processing using loops.
- Useful for marks, salaries, temperatures, and lists of numbers.
Declaration of an array
Array declaration specifies the data type, array name, and number of elements.
Syntax:
data_type array_name[array_size];
Examples:
int marks[5];
float prices[10];
char letters[20];
| Declaration | Meaning |
|---|---|
| int marks[5]; | Five integer elements |
| float prices[10]; | Ten floating-point elements |
| char letters[20]; | Twenty character elements |
For an array declared as int marks[5];, space is reserved for five integers.
Initialization of an array
Initialization means assigning initial values to the array elements.
Method 1: Initialization during declaration
int marks[5] = {85, 90, 75, 88, 95};
Method 2: Let the compiler determine the size
int numbers[] = {10, 20, 30, 40};
The compiler determines that numbers contains four elements.
Method 3: Partial initialization
int a[5] = {10, 20};
The elements are initialized as follows: 10, 20, 0, 0, 0
When an array is initialized with a brace-enclosed list containing fewer values than its size, the remaining elements are initialized to zero.
Important: An uninitialized local array such as int a[5]; does not automatically contain zeros. Its elements have indeterminate values.
Accessing array elements
Array elements are accessed using the array name and an index inside square brackets ([]).
Syntax:
array_name[index];
In C, array indexing starts from 0, not 1. For an array containing five elements:
| Element | Index | Example |
|---|---|---|
| First | 0 | marks[0] |
| Second | 1 | marks[1] |
| Third | 2 | marks[2] |
| Fourth | 3 | marks[3] |
| Fifth | 4 | marks[4] |
Example: C Program to access array elements
#include
int main(void)
{
int marks[5] = {85, 90, 75, 88, 95};
printf("First mark = %d\n", marks[0]);
printf("Third mark = %d\n", marks[2]);
printf("Last mark = %d\n", marks[4]);
return 0;
}
Output:
First mark = 85
Third mark = 75
Last mark = 95
Storing and retrieving values
Array elements can be modified after declaration.
int a[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;
printf("%d", a[1]);
Output:
20
Here, values are stored using assignment statements and retrieved using their indexes.
Storing and accessing elements using a loop
Loops are useful when an array contains many elements. Instead of writing separate statements for every element, we can use a loop.
#include
int main(void)
{
int a[5];
int i;
printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &a[i]);
}
printf("Array elements are:\n");
for (i = 0; i < 5; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Sample input:
10 20 30 40 50
Output:
Array elements are:
10 20 30 40 50
Notice the loop condition:
i < 5
It ensures that the loop uses indexes 0 through 4.
Index range checking in C
Index range checking means verifying that an array index is within the valid range before accessing an element.
For an array declared as:
int a[5];
The valid index range is:
0 ≤ index < 5
✅ Valid indexes: 0, 1, 2, 3, 4
These refer to elements within the array.
❌ Invalid indexes: -1, 5, 6, ...
These are outside the array's valid range.
Example of range checking
#include
int main(void)
{
int a[5] = {10, 20, 30, 40, 50};
int index = 3;
if (index >= 0 && index < 5)
{
printf("Element = %d\n", a[index]);
}
else
{
printf("Invalid array index\n");
}
return 0;
}
Output:
Element = 40
The condition checks whether the index is at least zero and less than five before accessing the element.
Important exam point: Standard C does not automatically check array bounds at runtime. Accessing an array outside its valid range causes undefined behavior. Always validate indexes when they come from user input or calculations.
Common mistakes to avoid
| ❌ Mistake | ✅ Correct approach |
|---|---|
| Assuming the first index is 1 | Start at index 0 |
| Using i <= 5 for a five-element array | Use i < 5 |
| Accessing a[5] in int a[5] | The last valid element is a[4] |
| Reading uninitialized array elements | Initialize them or store values before reading |
| Forgetting & in scanf("%d", &a[i]) | Pass the address of the element |