Array in C || C Language
×

Array in C

0 1652

An array in C language is a collection of the 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.

Float marks[200];

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


Decalaration 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 the floating-point type and size 20,10,200 respectively.


Initialisation of Array

You can initialize an array while declaration.

Example:

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, the first index display the rows and second index display columns of the array.


Multi dimensional 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:

 #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



Share:


Discount Coupons



Leave a Reply


Comments
    Waiting for your comments