Arrays are one of the most fundamental and versatile data structures in programming, especially in C. They provide a simple and efficient way to store and manage collections of data elements of the same type. Whether you’re working with integers, characters, or floating-point numbers, arrays enable you to organize and access these values using a single variable.
In C, an array’s strength lies in its contiguous memory allocation, which ensures fast access to elements using their index. For instance, an integer array int a[] = {1, 2, 3, 4, 5};
can store five integers, while a character array char c[] = {'a', 'b', 'c', 'd', 'e'};
holds five characters.
1.Array is a data structure.
2.Array is a simple and fast data structure that stores same type of elements
Example:
int a[]={1,2,3,4,5}; integer array
char c[]={‘a’,’b’,’c’,’d’,’e’}; character array
There are different aspects in an array:
Basic Terminologies present in the array:
Memory representation of array:In array memory are stored in continguous memory location. It stores one after the other .
Declaration of array:
int a[5]
char c[6];
float f[5]; .
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
datatype a [size] = {v1, v2, … vN};
initialize using the initializer list
arr[]={1,2,3,4,5};(compiler automatically deduce the size of the array.)
3. Array initialization after declaration (using loops):
datatype arrayname[] = {1,2,3,4,5};
You can assign values to an array after declaring it by using a loop, like this:
int a[];
for(int i=0;i<n;i++)
{
scanf(“%d”,a[i]);
}
Example program to different types of initialization:
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5,6,7,8}; // with declaring the size
int b[]={1,2,3,4,5,6,7,8}; // without declaring the size
int c[] //without size and initializer list
for(int i=0;i<5;i++)
{
c[i]=i*2;
}
}
we can access any element of an array in c using the array subscript operator [ ] and the index value i of the element.
Simple example:
#include<stdio.h>
int main()
{
int a={1,2,3,4,5,6};
printf(“%d”,a[1]);
printf(“%d”,a[2]);
return 0;
}
Array traversal is nothing but visiting the elements present in the array this can be acheieved through the loops.
#include<stdio.h>
int main()
{
int a={1,2,3,4,5,6};
for(int i=0;i<5;i++)
{
printf(“%d”,i)
}
return 0;
}
// C Program to illustrate element access using array
// subscript
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 15, 25, 35, 45, 55 };
// accessing element at index 2 i.e 3rd element
printf(“Element at arr[2]: %d\n”, arr[2]);
// accessing element at index 4 i.e last element
printf(“Element at arr[4]: %d\n”, arr[4]);
// accessing element at index 0 i.e first element
printf(“Element at arr[0]: %d”, arr[0]);
return 0;
}
The one dimensional array also known as 1-D array it has only single row.
// C Program to illustrate the use of 1D array
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
// 1d array initialization using for loop
for (int i = 0; i < 5; i++) {
arr[i] = i*2;
}
printf(“Elements of Array: “);
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++) {
printf(“%d “, arr[i]);
}
return 0;
}
It has both rows and columns
For Example: If we are storing in single array it is very difficult to store it requires a lot boxes but if we are two dimensional array we can stores in a single box.storing 5 rows of mangoes and 5 columns of apples then we use two dimensional array (in a single box);
Syntax to declare two dimensional array:
A[size][size];
Program for the two dimensional array:
Program:
#include<stdio.h>
int main()
{
int a[3][3];
for (int i = 0; i <=2; i++) {
for (int j= 0; j <= 2; j++) {
scanf(“%d”,&a[i][j]);
} //input the element
}
for (int p = 0; p <= 2;p++) {
for (int m= 0; m <=2; m++) {
printf(“%d”,a[p][m]);
}
}
}
Indian Institute of Embedded Systems – IIES