Array in C with its types and examples

C Array With Examples

An array in c is a collection of the elements having the same data type. It also defines the continuous memory location of the variable and which can be referred by the single variable.

Declaration of an Array

type variable_name[ ];

In the above example type denotes the data type of array and variable_name denotes the name of array.

Examples of some basic Array of specific data type.

int a[10];
float a[20];
double a[20];
char a[20];

How to declare and Initialize an Array:-

int a[5]={8,56,34,57,78};

int a[ ]= {3,5,6,8,9};

float a[ ]={13.5,23.7,45.12,-34.4,-12.8};

char a1[7]=”Sumit”;

Some Important point about array by taking above example

  • All array element would be present in contiguous memory Locations.
  • 4 byte for the integer variable. If array have 6 elements then it consume total 24 bytes memory.
  • Arrays indexing always starts from a[0].
  • In the above Example of c array, each array occupies indexes from a[0] to a[5].

In addition below I have also mentioned some properties of an array. So please have a look over it.

Properties of An Array

An Array has the following properties.

  • Elements of an array should be of a similar data type.
  • It takes memory at contiguous fashion.
  • An array elements are stored in a continuous fashion so that it can be randomly accessed.

Now together with properties lets have a eye over advantage and disadvantage of an Array.

Advantage of C Array

1) Code Optimization: Data can be accessed by very less code.

2) Ease of traversing: Traversing of an Array is very easy with for loop.

3) Ease of sorting: We can easily sort Array elements.

4) Random Access: Also we can access elements randomly.

5) Data Structure Implementation: Array helps to implement various data structures like linked lists, stack, queue, trees, graphs, etc.

6) Matrix Representation: With the help of 2 Array, Matrix can be represented.

Disadvantage of C Array

1). Time complexity: In insertion and deletion operation, Time complexity increases.

2). Memory Wastage: Because arrays are fixed in size so if no elements in an array, still it consumes all spaces.

3). Fixed In Size: Once you had declared the size of an array, it is not possible to increase it.

An array example in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>  
int main(){      
int a=0;    
int marks[4];//here is declaration of array      
marks[0]=60;//here is initialization of array    
marks[1]=10;    
marks[2]=44;    
marks[3]=8;      
//here is traversal of array    
for(a=0;a<4;a++){      
printf("%d \n",marks[a]);    
}//end of for loop    
return 0;  
}

Output

Types of Array in C

There are 2 types of C arrays

  1. One dimensional array
  2. Multi dimensional array
    • Two dimensional array
    • Three dimensional array
    • four dimensional array etc….

One Dimensional Array

This can be considered as array as a row, where elements are stored one after another.

Syntax : data-type arr_name[array_size];

data-type: It denotes the type of the elements in the array.

arr_name: Name of the array. It must be a valid identifier.

array_size: Number of elements an array can hold.

Program of ONE Dimensional Array implementation In C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main()
{
     int i;
     int arr[5] = {10,20,30,40,50};  // declaring and Initializing array in C
         
        /* Above array can also be initialized as below
        arr[0] = 10;
        arr[1] = 20;
        arr[2] = 30;
        arr[3] = 40;
        arr[4] = 50; */

 
     for (i=0;i<5;i++)
     {
           printf("value of arr[%d] is %d \n", i, arr[i]);
      }  
}

Output:

Program of Two Dimensional Array implementation In C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main()
{
   int i,j;
   // declaring and Initializing array
   int arr[2][2] = {10,20,30,40};
   /* Above array can also be initialized as below
      arr[0][0] = 10;
      arr[0][1] = 20;
      arr[1][0] = 30;
      arr[1][1] = 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]);
      }
   }
}

Leave a Reply