Home » C Tutorial » Array in C Programming with Examples

Array in C Programming with Examples

An array in c is a group of data items of the same data type that share a common name. It should be of a single type, comprising of integers or characters and so on. An array in c is a linear and homogeneous data structure. Linear data structure stores its individual data elements in sequential order in the memory. Homogeneous means all individual data elements are of the same data type. 

An array is also known as a ‘Subscripted Variable’. The reason is as follows. Only one name is assigned to an array and individual elements are referenced by specifying a subscript. A subscript is also called an index. In C, subscripts start at 0(zero) and cannot be negative.

Why do we need an array?

We all know how to store a single data item (it may be numerical or character constants) in memory using variables. But very often, we need to store a large amount of data where we process collections of related data items such as the addition of fifty integers, marks of students in a university, etc. In such cases, we have to declare and use a wide number of variables, which is very inconvenient.

C language provides an array for such problems. This enables the user to access any number of elements of relative data type using a single name and different subscripts.

Advantages of an array in c

The advantages of an array are as follows

  1. Accessing the stored data is very easy in the array using indexing.
  2. We can implement data structures like linked list, stack, queue, tree, graph, etc can be implemented.
  3. Using a two-dimensional array we can implement a matrix.
  4. Arrays are very easy to use and implement.

Disadvantages of an array in c

The disadvantages of an array are as follows

  1. Constant size To get an array of a different size, you must explicitly deal with memory using realloc, malloc etc. 
  2. Constant data type.
  3.  Large free sequential block to accommodate large arrays.
  4. Arrays do not support copy assignment (you can not write array marks = array scores) 
  5. Sequential Search – You can search any element in an unsorted array by the sequential search technique. Thus, if the element is located at the 5th position in the array, then we will have to first go through the first 4 elements before locating it in the 5th position. 
  6. Elements cannot be inserted into an array – Inserting an element into an array would mean shifting down by one place of all the elements after the correct position of the inserted element. 
  7. Elements cannot be deleted from an array – Deleting an element from an array would mean shifting up by one position of all the elements in the array.

Types of an array in c

There are 2 types of an array in c.

  1. 1-D arrays (one-dimensional array)
  2. Multi-Dimensional arrays (two-dimensional array, three-dimensional array, n-dimensional array)

1 – D arrays (One-dimensional arrays)

The concept of the one-dimensional array is a row where the data items are stored one after one.

Declaration of a one-dimensional array.

Syntax: datatype arrayname[size];

Where,

Datatype: The type of data stored in the array.

Array name: Name of the array.

Size: Maximum number of elements that an array can.

Example: int marks[10];

This example represents the marks of the 10 students.

In this example, we are representing a set of 10 student marks and the

computer allocates 10 storage locations as shown below:

mark[0]mark[1]…………………..mark[9]

Initialization of a one-dimensional array.

You can initialize an array in two ways.

1. Initializing array elements one by one.

Syntax : arrayname[index] = value;

Example

mark[0] = 34;
mark[1] = 87;
mark[2] = 63;
mark[3] = 98;
mark[4] = 25;
mark[5] = 57;
mark[6] = 83;
mark[7] = 29;
mark[8] = 46;
mark[9] = 92;

2. Initializing the whole array directly.

Syntax : datatype arrayname[] = {list of values};

Example

int mark[10] = {35,70,40,55,26,43,56,82,78,86};

This array is stored in the memory as follows.

35704055264356827886
mark[0] mark[1] mark[2] mark[3] mark[4] mark[5] mark[6] mark[7] mark[8] mark[9] 

The declaration and initialization of the character array.

Example : char a[8] = (‘L’, ‘E’, ‘A’, ‘R’, ‘N’,”, ‘C’};

This array is stored in the memory as follows.

‘L’‘E’‘A’‘R’‘N’‘’‘C’‘\0’
a[0]a[1]a[2]a[3]a[4]a[5]a[6]a[7]

When the compiler sees a character array, it terminates it with an additional null character. Thus, when declaring character arrays, we must always allow one extra element space for the null terminator.

2-D arrays (Two-dimensional arrays)

The need for two-dimensional arrays to store a table of values. Thus, it is referred to as a Matrix or a Table.

A matrix has two subscripts – The first subscript denotes the number of rows and the second subscript denotes the number of columns.

Declaration and syntax:

datatype arrayname[rows] [columns];

Where,

datatype: The type of data stored in the array.

arrayname: Name of the array.

rows: Maximum number of rows in the array.

columns: Maximum number of columns in the array.

Example

int value[2][3]; //implies 2 rows and 3 columns.

Initialization: int table[2][3] = {2,11,3,5,6,3};

This initialized 2-D array can be stored in the memory as follows.


Col 0Col 1Col 2
Row 02113

Col 0Col 1Col 2
Row 1563

3-D arrays (Three-dimensional arrays)

It takes 3 subscripts as follows.

int x[2][3][2];

In this example, x consists of 3 2-D arrays having 3 rows and 2 columns each. The diagrammatic representation of the 3-D array is as follows.

2D – array 0

15
27
68

2D – array 0

92
43
64

2D – array 0

52
46
73

Array programs in C

1. Accept one number from the user and print the table of the entered number.

Example

#include<stdio.h>
main()
{
	int n, i, array[100];
	printf("Enter number elements");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("Enter data [%d]",i+1);
		scanf("%d",&array[i]);
	}
	printf("Data stored in array is : ");
	for(i=0;i<n;i++)
	{
		printf("%d\t",array[i]);
	}
	
}

Output

Enter number elements10
Enter data [1]0
Enter data [2]9
Enter data [3]8
Enter data [4]7
Enter data [5]6
Enter data [6]5
Enter data [7]4
Enter data [8]3
Enter data [9]2
Enter data [10]1
Data stored in array is : 0     9       8       7       6       5       4       3       2       1

2. Accept data from the user and print it in a matrix format.

Example

#include<stdio.h>
main()
{
	int r, c, i, j, array[10][10];
	printf("Enter number of rows:");
	scanf("%d",&r);
	printf("Enter number of columns:");
	scanf("%d",&c);
	printf("Enter data in matrix:\n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			printf("Enter data of index[%d][%d]",i,j);
			scanf("%d",&array[i][j]);
		}
	}
	printf("The matrix is :\n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			printf("%d\t",array[i][j]);
		}
		printf("\n");
	}
}

Output

Enter number of rows:3
Enter number of columns:3
Enter data in matrix:
Enter data of index[0][0]1
Enter data of index[0][1]2
Enter data of index[0][2]3
Enter data of index[1][0]4
Enter data of index[1][1]5
Enter data of index[1][2]6
Enter data of index[2][0]7
Enter data of index[2][1]8
Enter data of index[2][2]9
The matrix is :
1        2       3
4       5       6
7       8       9

Dynamic array in C

To reduce wastage of memory, we can use dynamic memory allocation for storing data of the array in memory. 

Accept data from the user and store it dynamically. Print the stored data.

Example

#include <stdio.h> 
#include <stdlib.h> 
main() 
{ 
    int* ptr; 
    int n, i; 
    printf("Enter number of elements:");
	scanf("%d",&n); 
  
    ptr = (int*)calloc(n, sizeof(int)); 
  
    if (ptr == NULL) 
	{ 
        printf("Error in memory allocation"); 
        exit(0); 
    } 
    else { 
  
        printf("Memory successfully allocated\n"); 
  
        for (i = 0; i < n; i++) 
		{ 
            printf("Enter element of location [%d]",(i+1));
			scanf("%d",&ptr[i]); 
        } 
  
        printf("The elements of the array are: "); 
        for (i = 0; i < n; i++) { 
            printf("%d, ", ptr[i]); 
        } 
        
        free(ptr); 
    } 
  
} 

Output

Enter number of elements:5
Memory successfully allocated
Enter element of location [1]6
Enter element of location [2]7
Enter element of location [3]8
Enter element of location [4]9
Enter element of location [5]0
The elements of the array are: 6, 7, 8, 9, 0

FAQ’s

How to find array length in c?

Using counter we can count the length of an array
cnt = 0;
for(i=0;i<n;i++) //n is number of elements
{
cnt++;
}
printf(“Length of array is :%d”,cnt);

How to pass a 2d array to function in c?

Passing array to function is simple with using pointers. Also we can pass it directly.
#include<stdio.h>
func(array[10][10])
{
//statements;
}

main()
{
int array[10][10];
func(array);
}

How to initialize array in c?

we can initialize arrays in two ways.
1. Initializing each index individually.
Int
X[0] = 100;
X[1] = 200;
X[2] = 300;


2. Initializing the whole array at a time.
int x[5] = {100,200,300,400,500};

Do you know?
1. Fibonacci Series In C
2. Bubble Sort In C
3. Storage Classes in C
4. Array in C Programming

Pin It on Pinterest