An array is a collection of data items of the same type stored at contiguous memory locations. Arrays are derived data types in C that can store primitive data types (like int, char, double, float) as well as derived data types (like pointers, structures, etc.). Arrays are the simplest data structure, allowing random access to each data element using an index number.
-
Each element of the array has the same data type and size; for example, an int type occupies 4 bytes.
-
The elements of the array are stored in contiguous memory locations, with the first element stored at the lowest memory address.
-
You can randomly access the elements of the array because the address of each element can be calculated based on a given base address and the size of the data element.
Advantages of C Arrays:
-
Code Optimization: Less code is required to access data.
-
Easy to Traverse: You can easily access the elements of the array using a for loop.
-
Easy to Sort: Sorting the elements of an array can be done in just a few lines of code.
-
Random Access: You can access any element in the array at will.
π Click to Claim π
π Collection of C Language Knowledge Materials
Disadvantages of C Arrays:
Fixed Size: The size defined when declaring an array cannot exceed the limit. Therefore, it cannot grow dynamically like a linked list.
Declaring C Arrays:
You can declare an array in C language using the following syntax:
data_type array_name[array_size];
For example, here is a declaration of an array:
int marks[5];
Where int is the data type, marks is the array name, and 5 is the array size.
Initialization of C Arrays
The simplest way to initialize an array is to use the index number of each element. You can initialize each element of the array using its index. Consider the following example:
marks[0] = 80; // Array Initialization
marks[1] = 60;
marks[2] = 70;
marks[3] = 85;
marks[4] = 75;
Example of C Arrays:
#include <stdio.h>
int main() {
int i = 0;
int marks[5]; // Array Declaration
marks[0] = 80; // Array Initialization
marks[1] = 60;
marks[2] = 70;
marks[3] = 85;
marks[4] = 75;
// Traverse Array
for(i = 0; i < 5; i++) {
printf("%d \n", marks[i]);
}
return 0;
}
Output:
8060708575
Declaration and Initialization of C Arrays
You can initialize an array at the time of declaration. See the code below:
int marks[5] = {20, 30, 40, 50, 60};
In this case, there is no need to define the size. It can also be written as:
int marks[] = {20, 30, 40, 50, 60};
Letβs look at a program example in C for declaring and initializing an array:
#include <stdio.h>
int main() {
int i = 0;
int marks[5] = {20, 30, 40, 50, 60}; // Declare and Initialize Array
// Traverse Array
for(i = 0; i < 5; i++) {
printf("%d \n", marks[i]);
}
return 0;
}
Output:
2030405060
C Array Example: Sorting an Array
The following program uses the bubble sort method to sort an array in ascending order.
#include <stdio.h>
void main() {
int i, j, temp;
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i < 10; i++) {
for(j = i + 1; j < 10; j++) {
if(a[j] > a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i < 10; i++) {
printf("%d\n", a[i]);
}
}
Prints the sorted element list.
Program Example: Print the largest and second largest elements in the array.
#include <stdio.h>
void main() {
int arr[100], i, n, largest, sec_largest;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
sec_largest = arr[1];
for(i = 0; i < n; i++) {
if(arr[i] > largest) {
sec_largest = largest;
largest = arr[i];
} else if(arr[i] > sec_largest && arr[i] != largest) {
sec_largest = arr[i];
}
}
printf("largest = %d, second largest = %d", largest, sec_largest);
}
Programmer Technical Exchange Group
Scan the code to join the group, remember to note: city, nickname, and technical direction.