Array: Array in C is one of the most used data structures in C programming.
It is a simple and
fast way of storing multiple values under a single name.
An array in C is a fixed-size collection of similar data items stored in contiguous memory
locations. It can be used to store the collection of primitive data types such as int, char, float,
etc., and also derived and user-defined data types such as pointers, structures, etc.
Concept of Indexing: Indexing in C refers to the method of accessing individual elements of
arrays using their position (index). Arrays in C are zero-indexed, meaning the first element
has index 0, the second has index 1, and so on.
Syntax of Array Declaration:
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
C Array Initialization:
Initialization in C is the process to assign some initial value to the variable. When the array is
declared or allocated memory, the elements of the array contain some garbage value. So, we
need to initialize the array to some meaningful value. There are multiple ways in which we
can initialize an array in C.
1. Array Initialization with Declaration
In this method, we initialize the array along with its declaration. We use an initializer list to
initialize multiple elements of the array. An initializer list is the list of values enclosed within
braces { } separated b a comma.
Syntax:
data_type array_name [size] = {value1, value2, ... valueN};
2. Array Initialization with Declaration without Size
If we initialize an array using an initializer list, we can skip declaring the size of the array as
the compiler can automatically deduce the size of the array in these cases. The size of the
array in these cases is equal to the number of elements present in the initializer list as the
compiler can automatically deduce the size of the array.
Syntax:
data_type array_name[] = {1,2,3,4,5};
3. Array Initialization after Declaration (Using Loops)
We initialize the array after the declaration by assigning the initial value to each element
individually. We can use for loop, while loop, or do-while loop to assign the value to each
element of the array.
Syntax:
for (int i = 0; i < N; i++)
{
array_name[i] = value i;
}
Access Array Elements
We can access any element of an array in C using the array subscript operator [ ] and the
index value i of the element.
array_name [index];
One thing to note is that the indexing in the array always starts with 0, i.e., the first element is
at index 0 and the last element is at N – 1 where N is the number of elements in the array.
Update Array Element
We can update the value of an element at the given index i in a similar way to accessing an
element by using the array subscript operator [ ] and assignment operator =.
Syntax:
array_name[i] = new_value;
Types of Array in C:
There are two types of arrays based on the number of dimensions it has. They are as follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays
Syntax of 1D Array in C:
array_name [size];
1. One Dimensional Array in C:
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have only
one dimension.
2. Multidimensional Array in C:
Multi-dimensional Arrays in C are those arrays that have more than one dimension. Some of
the popular multidimensional arrays are 2D arrays and 3D arrays.
C array size and length:
To get the size of an array, you can use the sizeof operator:
#include <stdio.h>
int main()
{
int myNumbers[] = {10, 25, 50, 75, 100};
printf("%d", sizeof(myNumbers));
return 0;
}
output:
20
- It is because the sizeof operator returns the size of a type in bytes.
You learned from the data types that an int type is usually 4 bytes, so from the example
above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
C array length:
int myNumbers[] = {10, 25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);
printf("%d", length);
output:
5
Memory size of data types in C:
Data type Memory size
Char 1 byte
Int 4 bytes
Float 4 bytes
Void No storage
1. WAP to print the element of the array.
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
printf("The first element is: %d\n", numbers[0]);
printf("The third element is: %d\n", numbers[2]);
numbers[1] = 25;
printf("numbers[1] = %d\n", numbers[1]);
return 0;
}
2. WAP to add the element of the array.
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int i, sum = 0;
for (i = 0; i < 5; i++)
{
sum += arr[i]; // add each element
}
printf("Sum of array elements = %d\n", sum);
return 0;
}
3. WAP to show even number in array.
#include <stdio.h>
int main()
{
int arr[6] = {11, 22, 33, 44, 55, 66};
int i;
printf("Even numbers in array are:\n");
for (i = 0; i < 6; i++)
{
if (arr[i] % 2 == 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
4. WAP to show prime number in array.
#include <stdio.h>
int main()
{
int arr[6] = {2, 3, 4, 5, 10, 11};
int i, j, isPrime;
printf("Prime numbers in array are:\n");
for (i = 0; i < 6; i++)
{
if (arr[i] < 2) continue; // skip numbers < 2
isPrime = 1; // assume prime
for (j = 2; j <= arr[i] / 2; j++)
{
if (arr[i] % j == 0)
{
isPrime = 0; // not prime
break;
}
}
if (isPrime == 1)
{
printf("%d ", arr[i]);
}
}
return 0;
}
5. WAP to find the biggest element in array.
#include <stdio.h>
int main()
{
int arr[6] = {25, 78, 12, 56, 89, 45};
int i, max;
max = arr[0]; // assume first element is biggest
for (i = 1; i < 6; i++)
{
if (arr[i] > max)
{
max = arr[i]; // update max
}
}
printf("Biggest element in array = %d\n", max);
return 0;
}
6. WAP to enter marks of five subjects and calculate total and print the average using
array.
#include <stdio.h>
int main()
{
int marks[5];
int i, total = 0;
float average;
printf("Enter marks of 5 subjects:\n");
for (i = 0; i < 5; i++)
{
printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
total += marks[i];
}
average = total / 5.0;
printf("Total Marks = %d\n", total);
printf("Average Marks = %.2f\n", average);
return 0;
}