Array in C

An array in C language is a collection of same data types. The size of an array must be a constant value.

The syntax for C arrays:

int a[20]; // integer array
char b[10]; // character array




Example of Array

If anyone wants to store marks of 200 students, he/she can create an array for it.

For eg. Float marks[200];

Once size and type of the array are declared, it will be fixed.

Declaration of Array

Data-type array_name[array_size];

Example

Int a[20]; // integer array
char b[10]; // character array

float marks[200]; // float array

In these examples, we declared arrays a,b, marks of integer, character and floating-point type and size 20,10,200 respectively.

Initialisation of Array

You can initialize array while declaration.

For e.g.

int mark[2] ={24,15};

or

int mark[]={24,25};

Important Note

Suppose, we have declared an array of 5 elements. Let’s say,

Int marks [5];

We can use the array members from marks[0] to marks[4].If anyone tries to access out of this bound, there may occur unexpected output.

One-dimensional array

It is known as the linear array and while accessing it involves single subscripts that can be row or column.

Syntax: datatype name of an array[sizeofArray];

Int marks[5];

Two Dimensional Array

The Two-dimensional array is an extension of a one-dimensional array. These arrays consist of two subscripts and we often call it an array of the array also. In this, there is a division of array occurs i.e. rows and columns and applicable for handling tabular data.

Syntax: data_type array_name[row_size][column_size];

Example

int arr[4][4];

in this example, first index display the rows and second index display columns of the array.

Multidimensional array

The array of arrays is another name of multi-dimensional Array. It is identical to the two-dimensional array but it can be three-dimensional arrays.

Syntax

data_type array_name[size1][size2][size3]——[sizeN];

Example of an Array in C programming language is below:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 
 int i, number[5];
 clrscr();
 
 printf("Enter 5 number \n");
 for(i=0;i<5;i++)
 scanf("%d", %numbers[i]);
 
 printf("Array elements are \n");
 for(i=0;i<5;i++)
 
 printf("%d\n",numbers[i]);
 getch();
 return 0;

 }
 
 Result:
 4
 6
 7
 3
 2

Coding Tag

Coding Tag always happy to serve you in Information Technology (IT). Learn HTML, CSS, Javascript, PHP, Android, Digital Marketing, Latest IT News and many more.

Leave a Reply

avatar
  Subscribe  
Notify of