0 of 3 saved

C Language6 min read

Arrays

Read this chapter, write the program on this page, then pass a short check to open the next one.

In C language, arrays are reffered to as structured data types. An arrary is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.

Arrays in C

In this tutorial, we will be covering Arrays in C programming Language.

In C language, arrays are referred to as structured data types. It is defined as a finite ordered collection of homogenous data, stored in contiguous memory locations.

Here the words,

  • Finite means data range must be defined.

  • Ordered means data must be stored in contiguous memory addresses.

  • Homogenous means data must be of a similar data type.

An array is a set of data items of a similar data type that share a common name. Ordinary variables are efficient in holding only one value at a time. If we need to save more than one value in a single variable, then in that case we use arrays.

Also, an array is a collective name provided to the group of the same quantities. Each member in the group is referred by their position in the group.

Arrays are allotted memory in a strictly contiguous fashion. The simplest array is a one-dimensional array which is simply a list of variables of the same data type.

Example where arrays are used,

  • To store the list of Employee or Student names,

  • To store marks of students,

  • To store the list of numbers or characters etc.

Since arrays support an easy way to define data, it is classified amongst the data structures in C. Other data structures in C are the structure, lists, queues, trees, etc. An array can be used to represent not only a simple list of data but also the table of data in two or three dimensions.

Properties of Array

The array contains the following properties:

  • Each element of an array is of a similar data type and carries a similar size, i.e., int = 4 bytes.

  • Elements of the array are saved at contiguous memory locations where the first element is saved at the smallest memory location.

  • Elements of the array can be randomly created since we can evaluate every element of the array with the given base address and the size of the data element.

Declaring an Array in C

As we know that, the array stores elements of the same data type. Like any other data element, the declaration of an array is just before its use. The general form of array declaration is:

data-type variable-name[size];
/* Example of array declaration */
int arr[10];


Arrays in C

  • Here int is the data type and the data type mainly indicates the type of data that is going to be stored in the array. Thus data type mainly specifies the type of array elements.

  • arr is the name of the array.

  • 10 is the size of the array ( Also size indicates the maximum number of elements that can be stored inside the array). It means an array arr can only contain 10 elements of int type.

Index of an array starts from 0 to size-1, i.e. first element of arr array will be stored at the arr[0] address, and the last element will occupy the arr[9].

Thus the declaration of the array mainly provides the following information to the compiler:

  • Name of the Array

  • Type of the elements that are to be stored in the array.

  • Number of subscripts it has,

  • And the size of the Array

Initialization of an Array in C

An array can be initialized mainly by placing the elements of the array, separated by commas within the braces. The general form of initialization of the array is as follows:

data-type array-name[size] = { list of values };

After the declaration of the array, we should initialize it. Otherwise, it will contain garbage value(any random value). Initialization of an array can be done either at compile-time or at runtime.

Example: Array initialization in C

int var[6]={23,11,12,1,5,9};

The above statement will declare a variable named var of size 6 and will assign the values to the individual array element as follows:

var[0]=23
var[1]=11
var[2]=12
var[3]=1
var[4]=5
var[5]=9

All the individual array elements that are not assigned with explicit initial values, will automatically be set to zero.

Compile-time Array initialization

The initialization of array elements at the compile-time similar to the ordinary variable initialization. The general form of initialization of array is,

data-type array-name[size] = { list of values };

/* Here are a few examples */
int marks[4]={ 67, 87, 56, 77 };    // integer array initialization

float area[5]={ 23.4, 6.8, 5.5 };   // float array initialization

int marks[4]={ 67, 87, 56, 77, 59 };    // Compile time error

One important thing to remember is that when you will give more initializer(array elements) than the declared array size, then the compiler will give an error.

#include<stdio.h>

void main()
{
    int i;
    int arr[] = {2, 3, 4};      // Compile time array initialization
    for(i = 0 ; i < 3 ; i++) 
    {
        printf("%d\t",arr[i]);
    }
}


2 3 4

Runtime Array initialization

At runtime, an array can also be initialized by using scanf() function. The approach of runtime initialization of the array is mainly used for initializing large arrays or to initialize arrays with user-specified values. Example,

#include<stdio.h>

void main()
{
    int arr[4];
    int i, j;
    printf("Enter array element");
    for(i = 0; i < 4; i++)
    {
        scanf("%d", &arr[i]);    //Run time array initialization
    }
    for(j = 0; j < 4; j++)
    {
        printf("%d\n", arr[j]);
    }
}


Enter array element:4
1
2
3
4
1
2
3

Frequently Asked Questions

Ques 1: What are arrays in C?

An array is defined as a finite ordered collection of homogenous data, stored in contiguous memory locations.

Ques 2: What is array syntax?

The array stores elements of the same data type. Like any other data element, the declaration of an array is just before its use. The general form of array declaration is:

data-type variable-name[size];
/* Example of array declaration */
int arr[10];

where int indicates that array named arr is of int type having size 10.

Ques 3: Give Examples where arrays are used?

Arrays are used in the following cases:

  • to store the list of Employee or Student names,

  • to store marks of students,

  • or to store the list of numbers or characters, etc.

Ques 4: What are the advantages of using an array?

Given below are some of the advantages of using an array:

  • With the help of Arrays, multiple data items of the same type can be represented using a single name.

  • The elements of an array can be accessed easily just by using the index number.

  • The Arrays allocate memory in contiguous memory locations for all the elements.

  • Arrays avoid the concept of memory overflow or shortage of memory.

Conclusion

This tutorial was all about an Introduction to arrays in the C programming language. We have covered a basic introduction to an Array, its properties, declaration of an array, and initialization of an array along with the examples for more clear understanding.