Arrays in C
An array is a collection of multiple values of the same data type stored in contiguous (adjacent) memory locations, all referenced by a single variable name. Instead of creating separate variables for each value, an array stores them all under one name and accesses each using an index number.
Think of an array like a row of numbered lockers. Each locker holds one item, each locker has a number (index), and they all belong to the same row (array name).
Why Use Arrays?
Without arrays, storing 50 student marks would require 50 separate variables:
int m1, m2, m3, ... m50; // very impractical!
With an array, it becomes simple:
int marks[50]; // one array holds all 50 marks
Declaring an Array
Syntax
data_type array_name[size];
- data_type — the type of elements stored (int, float, char, etc.)
- array_name — the name of the array (follows identifier rules)
- size — the total number of elements the array can hold
Examples
int scores[5]; // array of 5 integers
float prices[10]; // array of 10 floats
char vowels[5]; // array of 5 characters
Array Indexing
Each element in an array is accessed by its index (position). Array indexing in C starts from 0, not 1.
int scores[5];
// Index: 0 1 2 3 4
// Positions: [first][second][third][fourth][fifth]
So for an array of size 5, valid indices are 0, 1, 2, 3, and 4.
Initializing Arrays
Method 1 — Initialize at Declaration
int scores[5] = {90, 75, 88, 60, 95};
Method 2 — Initialize Without Specifying Size (Compiler determines size)
int scores[] = {90, 75, 88, 60, 95}; // size = 5 (auto)
Method 3 — Initialize to Zero
int scores[5] = {0}; // all elements set to 0
int count[10] = {}; // also sets all to 0
Method 4 — Assign Values Using Index
int scores[5];
scores[0] = 90;
scores[1] = 75;
scores[2] = 88;
scores[3] = 60;
scores[4] = 95;
Accessing Array Elements
#include <stdio.h>
int main()
{
int scores[5] = {90, 75, 88, 60, 95};
printf("First score : %d\n", scores[0]); // 90
printf("Second score : %d\n", scores[1]); // 75
printf("Last score : %d\n", scores[4]); // 95
return 0;
}
Output
First score : 90
Second score : 75
Last score : 95
Traversing an Array Using a Loop
The most common way to access all elements of an array is using a for loop.
#include <stdio.h>
int main()
{
int marks[5] = {78, 85, 92, 60, 74};
int i;
printf("Student Marks:\n");
for (i = 0; i < 5; i++)
{
printf("Student %d: %d\n", i + 1, marks[i]);
}
return 0;
}
Output
Student Marks:
Student 1: 78
Student 2: 85
Student 3: 92
Student 4: 60
Student 5: 74
Finding Sum and Average of Array Elements
#include <stdio.h>
int main()
{
int nums[6] = {10, 20, 30, 40, 50, 60};
int sum = 0;
float avg;
for (int i = 0; i < 6; i++)
{
sum += nums[i];
}
avg = (float)sum / 6;
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", avg);
return 0;
}
Output
Sum = 210
Average = 35.00
Finding Maximum and Minimum in an Array
#include <stdio.h>
int main()
{
int nums[5] = {34, 12, 78, 55, 23};
int max = nums[0];
int min = nums[0];
for (int i = 1; i < 5; i++)
{
if (nums[i] > max)
max = nums[i];
if (nums[i] < min)
min = nums[i];
}
printf("Maximum = %d\n", max); // 78
printf("Minimum = %d\n", min); // 12
return 0;
}
Taking Array Input from User
#include <stdio.h>
int main()
{
int arr[5];
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("\nYou entered: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Two-Dimensional Arrays (2D Arrays)
A 2D array is essentially a table (matrix) with rows and columns. It is useful for storing data in grid format like a matrix or a chessboard.
Syntax
data_type array_name[rows][columns];
Example — 3x3 Matrix
#include <stdio.h>
int main()
{
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("Matrix:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Matrix:
1 2 3
4 5 6
7 8 9
2D Array Memory Layout
matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3
matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6
matrix[2][0] = 7 matrix[2][1] = 8 matrix[2][2] = 9
Addition of Two Matrices
#include <stdio.h>
int main()
{
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
printf("Sum of matrices:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Output
Sum of matrices:
6 8
10 12
Important Notes About Arrays in C
- Array indices start from 0 and go up to size - 1.
- Accessing an index outside the valid range causes undefined behavior — no automatic error is shown, but the program may crash or give wrong results.
- The size of an array must be a positive integer constant (or a variable in C99).
- Arrays in C are not bounds-checked — it is the programmer's responsibility to stay within bounds.
- An array name itself holds the address of the first element (used with pointers).
Summary of Array Operations
| Operation | Syntax |
|---|---|
| Declare | int arr[5]; |
| Initialize | int arr[5] = {1,2,3,4,5}; |
| Access element | arr[2] |
| Modify element | arr[2] = 10; |
| Traverse all | for (i=0; i<5; i++) { printf("%d", arr[i]); } |
Summary
Arrays are one of the most important data structures in C. They allow storing multiple values of the same type under one name, making programs more organized and efficient. 1D arrays are used for lists of values, while 2D arrays are used for tables and matrices. Always remember that C arrays start at index 0, and going out of bounds leads to unexpected behavior. Arrays also form the foundation for strings and more advanced data structures like stacks, queues, and matrices.
