An array is a collection of elements of the same data type stored together. Arrays are useful when we need to store multiple values using a single variable name.
0, the second at index 1, and so on.
An array stores multiple values of the same type under one variable name.
int marks[5];
The array marks can store five integer values.
Suppose we want to store the marks of five students. Without an array, we could create five separate variables.
int mark1;
int mark2;
int mark3;
int mark4;
int mark5;
An array makes this easier:
int marks[5];
The basic syntax for declaring an array is:
data_type array_name[size];
Example:
int numbers[5];
int is the data type.numbers is the array name.5 is the number of elements.int numbers[5];
This creates an array capable of storing five integer elements.
The indexes are:
0 1 2 3 4
Each element of an array has an index. The first index is always
0.
int numbers[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
The last index of an array with five elements is 4.
An array can be initialized when it is declared.
int numbers[5] = {10, 20, 30, 40, 50};
The values are assigned to the array elements from left to right.
When an array is initialized with values, its size can be omitted.
int numbers[] = {10, 20, 30, 40};
The compiler determines the number of elements from the initializer. Here, the array contains four elements.
int numbers[] = {10, 20, 30};
printf("%d", numbers[0]);
Output:
10
The expression numbers[0] accesses the first element.
int numbers[] = {10, 20, 30, 40};
printf("%d\n", numbers[0]);
printf("%d\n", numbers[1]);
printf("%d\n", numbers[2]);
printf("%d\n", numbers[3]);
Output:
10
20
30
40
int numbers[] = {10, 20, 30};
numbers[1] = 100;
printf("%d", numbers[1]);
Output:
100
Array elements can be changed using their indexes.
Traversing means visiting each element of an array, usually with a loop.
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
The loop accesses every element from index 0 to
4.
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++)
{
printf("numbers[%d] = %d\n",
i, numbers[i]);
}
return 0;
}
A loop can be used to take input for each array element.
int numbers[5];
for (int i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
Each input value is stored at the corresponding index.
#include <stdio.h>
int main()
{
int numbers[5];
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("Array elements:\n");
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
int numbers[] = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += numbers[i];
}
printf("Sum = %d", sum);
Output:
Sum = 150
int numbers[] = {25, 10, 45, 30, 20};
int largest = numbers[0];
for (int i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest = %d", largest);
The first element is used as the initial largest value.
int numbers[] = {25, 10, 45, 30, 20};
int smallest = numbers[0];
for (int i = 1; i < 5; i++)
{
if (numbers[i] < smallest)
{
smallest = numbers[i];
}
}
printf("Smallest = %d", smallest);
int numbers[] = {10, 15, 20, 25, 30};
int count = 0;
for (int i = 0; i < 5; i++)
{
if (numbers[i] % 2 == 0)
{
count++;
}
}
printf("Even numbers = %d", count);
The modulo operator is used to check whether each number is even.
int numbers[] = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += numbers[i];
}
float average = sum / 5.0f;
printf("Average = %.2f", average);
Using 5.0f makes the division floating-point.
Arrays can store other data types such as float.
float prices[4] = {
10.5f,
20.5f,
30.5f,
40.5f
};
for (int i = 0; i < 4; i++)
{
printf("%.2f\n", prices[i]);
}
A character array stores characters.
char letters[5] = {'A', 'B', 'C', 'D', 'E'};
for (int i = 0; i < 5; i++)
{
printf("%c ", letters[i]);
}
Character arrays are also used to represent strings when a terminating null character is included.
For an array declared in the same scope, sizeof can be used
to calculate the total number of bytes occupied by the array.
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Elements = %d", size);
The result is 5.
An array can be passed to a function for processing.
void display(int numbers[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", numbers[i]);
}
}
int main()
{
int numbers[] = {10, 20, 30};
display(numbers, 3);
return 0;
}
The size is commonly passed separately.
void change(int numbers[])
{
numbers[0] = 100;
}
int main()
{
int numbers[] = {10, 20, 30};
change(numbers);
printf("%d", numbers[0]);
return 0;
}
Output:
100
When an array is passed to a function, the function can modify its elements through the parameter.
Never access an index outside the valid range of an array.
int numbers[5];
numbers[0] = 10;
numbers[4] = 50;
/* Invalid access */
numbers[5] = 60;
For an array of five elements, valid indexes are only
0 through 4. C does not perform automatic
bounds checking.
If an array is initialized with fewer values than its specified size, the remaining elements are initialized to zero.
int numbers[5] = {10, 20};
The elements are effectively:
10 20 0 0 0
#include <stdio.h>
int main()
{
int numbers[5];
int sum = 0;
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
for (int i = 0; i < 5; i++)
{
sum += numbers[i];
}
printf("Sum = %d", sum);
return 0;
}
#include <stdio.h>
int main()
{
int numbers[5];
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
int largest = numbers[0];
for (int i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest = %d", largest);
return 0;
}
int numbers[5];
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
For a local array like this, the elements are not automatically initialized to zero.
Practice the following programs:
Practice using loops with arrays because loops are commonly used to traverse array elements.
n is n - 1.sizeof can help calculate the number of elements of an array in its original scope.Question: What is the index of the first element of a C array?