Suppose you need to store the marks of 60 students. Declaring 60 separate variables would be painful, and you could not loop over them. An array solves this. It stores many values of the same type under one name, and you reach each value by its position.
This page shows how to declare and initialise an array in C, read values into it, loop through it and pass it to a function. It ends with an introduction to 2D arrays. For the language-neutral idea of an array, see what is an array.
Array declaration in C
type name[size];
int marks[5]; /* 5 ints */
float prices[10]; /* 10 floats */
char grade[3]; /* 3 chars */
int marks[5]; creates five int boxes, one after another in memory:
Index: [0] [1] [2] [3] [4]
+------+------+------+------+------+
marks: | | | | | |
+------+------+------+------+------+
Address: 1000 1004 1008 1012 1016
The addresses are only for the picture. Each step is 4 bytes because an int is usually 4 bytes. The valid indexes are 0 to 4. There is no marks[5].
Common mistake: A local array declared without values, such as
int marks[5];insidemain, holds garbage values until you fill it. Do not add up or print an array before you have stored something in every element.
Array initialisation in C
You can give values at the time of declaration.
#include <stdio.h>
int main(void) {
int marks[5] = {78, 85, 62, 90, 71};
int partial[5] = {1, 2};
int zeros[5] = {0};
int auto_size[] = {4, 8, 15};
int i;
for (i = 0; i < 5; i++) {
printf("%d ", partial[i]);
}
printf("\n");
for (i = 0; i < 5; i++) {
printf("%d ", zeros[i]);
}
printf("\n");
printf("marks[0] = %d, marks[4] = %d\n", marks[0], marks[4]);
printf("auto_size has %zu elements\n", sizeof(auto_size) / sizeof(auto_size[0]));
return 0;
}
Output:
1 2 0 0 0
0 0 0 0 0
marks[0] = 78, marks[4] = 71
auto_size has 3 elements
| Declaration | Result |
|---|---|
int marks[5] = {78, 85, 62, 90, 71}; |
All five values given |
int partial[5] = {1, 2}; |
1 2 0 0 0. Missing values become 0 |
int zeros[5] = {0}; |
All zeros. A quick way to clear an array |
int auto_size[] = {4, 8, 15}; |
Size is worked out from the list: 3 |
sizeof(auto_size) is the size of the whole array in bytes (12 with 4-byte ints) and sizeof(auto_size[0]) is the size of one element (4), so dividing gives the number of elements.
Accessing and changing elements
#include <stdio.h>
int main(void) {
int marks[5] = {78, 85, 62, 90, 71};
marks[2] = 66;
marks[4] = marks[4] + 5;
printf("Third mark: %d\n", marks[2]);
printf("Last mark: %d\n", marks[4]);
return 0;
}
Output:
Third mark: 66
Last mark: 76
marks[2] is the third element because counting starts at 0. You can use an array element anywhere you can use a normal variable.
Looping through an array
A for loop from 0 to size - 1 visits every element.
#include <stdio.h>
int main(void) {
int marks[5] = {78, 85, 62, 90, 71};
int i, total = 0;
for (i = 0; i < 5; i++) {
printf("marks[%d] = %d\n", i, marks[i]);
total += marks[i];
}
printf("Total = %d\n", total);
printf("Average = %.2f\n", total / 5.0);
return 0;
}
Output:
marks[0] = 78
marks[1] = 85
marks[2] = 62
marks[3] = 90
marks[4] = 71
Total = 386
Average = 77.20
The condition is i < 5, not i <= 5. Using <= would read marks[5], which is outside the array.
Array input in C
To read values from the user, call scanf inside a loop. Each element needs its address, so write &nums[i].
#include <stdio.h>
int main(void) {
int nums[100];
int n, i, sum = 0, largest;
printf("How many numbers (1-100)? ");
scanf("%d", &n);
printf("Enter %d numbers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
largest = nums[0];
for (i = 0; i < n; i++) {
sum += nums[i];
if (nums[i] > largest) {
largest = nums[i];
}
}
printf("Sum = %d\n", sum);
printf("Largest = %d\n", largest);
printf("In reverse: ");
for (i = n - 1; i >= 0; i--) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
Sample run:
How many numbers (1-100)? 5
Enter 5 numbers: 12 45 7 30 22
Sum = 116
Largest = 45
In reverse: 22 30 7 45 12
The array has room for 100 numbers, but only the first n are used. A real program should also check that n is between 1 and 100 before the loop. The largest-element logic is explained step by step in find the largest element in an array.
Searching an array
#include <stdio.h>
int main(void) {
int nums[6] = {12, 45, 7, 30, 22, 9};
int key = 30, i, found = -1;
for (i = 0; i < 6; i++) {
if (nums[i] == key) {
found = i;
break;
}
}
if (found != -1) {
printf("%d found at index %d\n", key, found);
} else {
printf("%d not found\n", key);
}
return 0;
}
Output:
30 found at index 3
This is called linear search. It checks elements one by one and stops at the first match with break.
Passing an array to a function
#include <stdio.h>
int sum_array(int arr[], int n) {
int i, total = 0;
for (i = 0; i < n; i++) {
total += arr[i];
}
return total;
}
int main(void) {
int marks[5] = {78, 85, 62, 90, 71};
printf("Total = %d\n", sum_array(marks, 5));
return 0;
}
Output:
Total = 386
Two rules:
- Pass the size as a separate argument. Inside
sum_array,arris really a pointer to the first element, sosizeof(arr)gives the size of a pointer, not of the array. - The function works on the original array, not a copy. If it changes
arr[0],marks[0]inmainchanges too. The reason is covered in what is a pointer in C.
2D array in C
A 2D array is a table with rows and columns. int m[2][3]; has 2 rows and 3 columns.
col 0 col 1 col 2
row 0: m[0][0] m[0][1] m[0][2]
row 1: m[1][0] m[1][1] m[1][2]
Use one loop for rows and a loop inside it for columns.
#include <stdio.h>
int main(void) {
int m[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int r, c, row_sum;
for (r = 0; r < 2; r++) {
row_sum = 0;
for (c = 0; c < 3; c++) {
printf("%d ", m[r][c]);
row_sum += m[r][c];
}
printf("| row sum = %d\n", row_sum);
}
return 0;
}
Output:
1 2 3 | row sum = 6
4 5 6 | row sum = 15
Matrix addition with 2D arrays
#include <stdio.h>
int main(void) {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2];
int r, c;
for (r = 0; r < 2; r++) {
for (c = 0; c < 2; c++) {
sum[r][c] = a[r][c] + b[r][c];
}
}
for (r = 0; r < 2; r++) {
for (c = 0; c < 2; c++) {
printf("%3d", sum[r][c]);
}
printf("\n");
}
return 0;
}
Output:
6 8
10 12
%3d prints each number in a space 3 characters wide, which keeps the columns lined up. To read a matrix from the user, use the same two loops with scanf("%d", &a[r][c]);.
In memory, a 2D array is stored row by row in one long block: m[0][0], m[0][1], m[0][2], then m[1][0] and so on.
Common mistakes with arrays in C
| Mistake | Example | What goes wrong |
|---|---|---|
| Index out of range | for (i = 0; i <= 5; i++) on int a[5] |
Reads a[5], which does not exist |
| Using values before filling | int a[5]; printf("%d", a[0]); |
Prints a garbage value |
Forgetting & in scanf |
scanf("%d", a[i]); |
Passes a value instead of an address, often a crash |
| Assigning one array to another | b = a; |
Compile error. Copy element by element in a loop |
Comparing arrays with == |
if (a == b) |
Compares addresses, not contents |
Using sizeof on a parameter |
sizeof(arr) inside a function |
Gives the pointer size, not the array size |
Quick revision
int a[5];creates 5 ints with indexes 0 to 4.- Missing values in an initialiser list become 0.
- Loop with
for (i = 0; i < n; i++). - Read input with
scanf("%d", &a[i]);inside a loop. - Pass the size along with the array to a function.
- A 2D array
m[rows][cols]needs two nested loops.
Arrays are the base for strings, sorting and searching. Next, learn strings in C, which are arrays of characters. When you need a collection that can grow and shrink, read array vs linked list.
Frequently Asked Questions
What is an array in C in simple words?
Why does array index start from 0 in C?
Can we change the size of an array in C?
What happens if I access an index outside the array?
How do I find the length of an array in C?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

