Header Ads

ARRAYS IN C

ARRAY


C Array is a collection of variables belonging to the same data type. You can store group of
data of same data type in an array.
Array might be belonging to any of the data types
Array size must be a constant value.
Always, Contiguous (adjacent) memory locations
are used to store array elements in memory.
It is a best practice to initialize an array to zero
or null while declaring, if we don’t assign any
values to array.

Example for C Arrays:

int a[10]; // integer array
char b[10]; // character array i.e. string

Types of C arrays:
There are 2 types of C arrays. They are:

1. One dimensional array
2. Multidimensional array
a. Two dimensional array
b. Three dimensional array, four dimensional array
etc…

1. One dimensional array in C:

Example program for one dimensional array in C:

#include<stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50};

for (i=0;i<5;i++)
{

printf(“value of arr[%d] is %d \n”,i, arr[i]);
}
return 0;
}

Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
2. Two dimensional array in C:
Two dimensional array is nothing but array of
array.


Example program for two dimensional array in C:

#include<stdio.h>
int main()
{
int i,j;    // declaring and Initializing array
int arr[2][2] = {10,20,30,40};    
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{

printf(“value of arr[%d] [%d] : %d\n”,i,j,arr[i][j]);
}
}
return 0;
}

Output:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40

No comments