![]() |
How To Use Array in C Language With Examples |
How To Use Array in C Language With Examples
Array Definition: Array is a collection of data of the same data type. It can be used to store groups of data simultaneously.
An array can store data of the same data type means an integer array can store only integer values, a character array can store only character values, and so on.
We can not fetch knowledge from an array directly thus we tend to use index purposes.
The array could be an assortment of comparable styles of parts that have a contiguous memory location.
The array is an Associate in the Nursing object that contains components of an identical information kind. it's an information structure wherever we have a tendency to store similar components.
we will store solely a hard and fast set of components during an array. The array is index-based, the primary component of the array holds on at the zero indexes.
Types of Array in C
There are two types of Array.
1. Single Dimensional Array
2.Multidimensional Array
Advantages of Array in C
Code Optimization: Array makes the code optimized, we can retrieve or sort data efficiently.
Random access: we can get any knowledge placed at an index position.
Disadvantages of Array in C
Size Limit: Array can store only the fixed size of elements in the array.
Syntax of Array
int ar[]=new int[5];
int ar[]=new int[5];
Here ar is the name of the array.
int is the data type of array.
The size of the array is five suggests that we can store most five values during this array.
How To Initialization of array in C?
int ar[]={10,20,30,40,50};
int ar[]=new int[5];
ar[0]=50;
ar[1]=40;
ar[2]=30;
ar[3]=20;
ar[4]=10;
int ar[]={10,20,30,40,50};
int ar[]=new int[5];
ar[0]=50;
ar[1]=40;
ar[2]=30;
ar[3]=20;
ar[4]=10;
How To Use Array in C Program?
#include<studio.h>
int main()
{
int ar[5]={ 80,77,14,78,01 };
printf("value is ar[0]: %d\n",ar[0]);
printf("value is ar[1]:%d\n",ar[1]);
printf("value is ar[2]:%d\n",ar[2]);
printf("value is ar[3]:%d\n",ar[3]);
printf("value is ar[4]:%d\n",ar[4]);
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
#include<studio.h>
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
How To Use Loop Array in C Program?
#include<stdio.h>
int main ()
{
int ar[]={80,77,14,78,01};
for(int i=0;i<5;i++)
{
printf("value is ar[%d]:%d\n",i,ar[i]);
}
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
#include<stdio.h>
int main ()
{
int ar[]={80,77,14,78,01};
for(int i=0;i<5;i++)
{
printf("value is ar[%d]:%d\n",i,ar[i]);
}
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
How To Use User input Method Array in C Program?
#include<studio.h>
int main()
{
int ar[5];
printf("Enter element 1:");
scanf("%d",&ar[0]);
printf("Enter element 2:");
scanf("%d",&ar[1]);
printf("Enter element 3:");
scanf("%d",&ar[2]);
printf("Enter element 4:");
scanf("%d",&ar[3]);
printf("Enter element 5:");
scanf("%d",&ar[4]);
for(int i=0;i<=4;i++)
{
printf("value is a[%d]=%d\n",i,ar[i]);
}
}
*****OUTPUT*****
Enter element 1:80
Enter element 2:77
Enter element 3:14
Enter element 4:78
Enter element 5:01
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
#include<studio.h>
*****OUTPUT*****
Enter element 1:80
Enter element 2:77
Enter element 3:14
Enter element 4:78
Enter element 5:01
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
Two Dimensional Array
Array: Array is a collection of data of the same data type. It can be used to store groups of data simultaneously.
The array can store data of the same data type means an integer array can store only integer value character array can store only character value and so on.
Important Others Programming Language Array
👉C++ Array
👉Java Array
We can not fetch data from the array directly therefore we use index point.
The array is a collection of similar types of elements that have a contiguous memory location.
An array is an object which contains elements of a similar data type. It is a data structure where we store similar elements.
We can store only a fixed set of elements in an array.
The array is index-based, the first element of the array is stored at the 0 indexes.
Syntax of Two Dimensional C Array
int ar[][]=new int[3][3];
int ar[][]=new int[3][3];
Here ar is the name of the array.
int is the data type of array.
The size of the array is 3x3 means we can store a maximum of 9 values in this array.
How To Initialization 2-D array in C?
int ar[][]=new int[3][3];
ar[0][0]=80;
ar[0][1]=77;
ar[0][2]=14;
ar[1][0]=78;
ar[1][1]=01;
ar[1][2]=96;
ar[2][0]=90;
ar[2][1]=40;
ar[2][2]=23;
int ar[][]=new int[3][3];
ar[0][0]=80;
ar[0][1]=77;
ar[0][2]=14;
ar[1][0]=78;
ar[1][1]=01;
ar[1][2]=96;
ar[2][0]=90;
ar[2][1]=40;
ar[2][2]=23;
How To Use 2-D Array in C Program?
#include<stdio.h>
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},
{90,40,23}};
printf("value is a[0][0]:%d\n",ar[0][0]);
printf("value is a[0][1]:%d\n",ar[0][1]);
printf("value is a[0][2]:%d\n",ar[0][2]);
printf("value is a[1][0]:%d\n",ar[1][0]);
printf("value is a[1][1]:%d\n",ar[1][1]);
printf("value is a[1][2]:%d\n",ar[1][2]);
printf("value is a[2][0]:%d\n",ar[2][0]);
printf("value is a[2][1]:%d\n",ar[2][1]);
printf("value is a[2][2]:%d\n",ar[2][2]);
}
*****OUTPUT*****
value at a[0][0]:80
value at a[0][1]:77
value at a[0][2]:14
value at a[1][0]:78
value at a[1][1]:01
value at a[1][2]:96
value at a[2][0]:90
value at a[2][1]:40
value at a[2][2]:23
#include<stdio.h>
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},
{90,40,23}};
printf("value is a[0][0]:%d\n",ar[0][0]);
printf("value is a[0][1]:%d\n",ar[0][1]);
printf("value is a[0][2]:%d\n",ar[0][2]);
printf("value is a[1][0]:%d\n",ar[1][0]);
printf("value is a[1][1]:%d\n",ar[1][1]);
printf("value is a[1][2]:%d\n",ar[1][2]);
printf("value is a[2][0]:%d\n",ar[2][0]);
printf("value is a[2][1]:%d\n",ar[2][1]);
printf("value is a[2][2]:%d\n",ar[2][2]);
}
*****OUTPUT*****
value at a[0][0]:80
value at a[0][1]:77
value at a[0][2]:14
value at a[1][0]:78
value at a[1][1]:01
value at a[1][2]:96
value at a[2][0]:90
value at a[2][1]:40
value at a[2][2]:23
How To Use Loop. 2-D Array in C Program?
#include<stdio.h>
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},{90,40,23}};
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
printf("%d ",ar[i][j]);
}
printf("\n");
}
}
*****OUTPUT*****
80 77 14
78 01 96
90 40 23
#include<stdio.h>
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},{90,40,23}};
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
printf("%d ",ar[i][j]);
}
printf("\n");
}
}
*****OUTPUT*****
80 77 14
78 01 96
90 40 23
How To Use User input Method 2-D Array in C Program?
#include<stdio.h>
int main()
{
int a[3][3];
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("Enter Element:a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
*****OUTPUT*****
Enter Element:a[0][0]:80
Enter Element:a[0][1]:77
Enter Element:a[0][2]:14
Enter Element:a[1][0]:78
Enter Element:a[1][1]:01
Enter Element:a[1][2]:96
Enter Element:a[2][0]:90
Enter Element:a[2][1]:40
Enter Element:a[2][2]:23
80 77 14
78 01 96
90 40 23
#include<stdio.h>
int main()
{
int a[3][3];
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("Enter Element:a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
*****OUTPUT*****
Enter Element:a[0][0]:80
Enter Element:a[0][1]:77
Enter Element:a[0][2]:14
Enter Element:a[1][0]:78
Enter Element:a[1][1]:01
Enter Element:a[1][2]:96
Enter Element:a[2][0]:90
Enter Element:a[2][1]:40
Enter Element:a[2][2]:23
80 77 14
78 01 96
90 40 23