Arrays in c

Arrays in c

Arrays in c : is collection of items.

Array is a collection of same data type (arrays in c).

What is array 

Array Definition – Array is a group if similar data items.

It stores homogeneous collection of elements that is it stores data of same data..

Array is derived data type.

Suppose if we want to store 10 value of integer type instead of using 10 different variable for storing 10 value we use single variable to store 10 value in one array of integer type.

Array can be of any data type.

Types of array

  1. One dimensional array
  2. Two dimensional array
  3. Multi dimensional array

1. One dimensional array : It is a collection of items of similar data type

Array declaration in c

Before using array we first have to declare that array.

Array syntax in c :-  <datatype> array_name[size];

In declaration of array , we first specify data type of array follows by name of array and size in bracket specifies number of elements we want to store in array.

Example:- int a[5];

Initialization of array

After declaration we initialize the array with values.

Array syntax in c :- <datatype> array name[size]={values};

Every element in array has index and index always starts with zero.we access any elements by its index.

Array stored sequentially in continuous memory.

Important points about array initialization

  1. If array nit initialized then elements contains garbage value.
  2. Array can be initialized with only constants.

Example:- int a[5]={1,2,3,4,5};

Accessing one dimensional array

A[0] => 1 is accessed

A[1] => 2 is accessed

A[2] => 3 is accessed

A[3] => 4 is accessed

A[4] => 5 is accessed

one dimensional array
one dimensional array

One dimensional array programs in c

12345678910111213141516171819#include <stdio.h>int main(){    int i; //Array declaration    printf(“Array values\n”); int arr[5]={1,2,3,4,5};  //Array initialization for(i=0;i<5;i++) { printf(“%d\n”,arr[i]);}    return 0;}

Output:-

One dimensional array
One dimensional array

Explanation :

1. Start with writing header file.

2. Next write main function.

3. Declare variable

i => temporary variable

4. Initialize array with 5 elements in array

int arr[5]={1,2,3,4,5};

5. now we print the values i stored in array. we use for loop to read array values from 0 index to end. (array index always starts with 0)

for(i=0;i<5;i++)

At iteration 1 :

i = 0 ,0<5 condtion is true and for loop executed

printf(“%d\n”,arr[i]);  => 1

At iteration 2 :

i = 1 ,1<5 condtion is true and for loop executed

printf(“%d\n”,arr[i]);  =>2

At iteration 3 :

i = 2 ,2<5 condtion is true and for loop executed

printf(“%d\n”,arr[i]); => 3

At iteration 4 :

i = 3 ,3<5 condtion is true and for loop executed

printf(“%d\n”,arr[i]); => 4

At iteration 5 :

i = 4 ,4<5 condtion is true and for loop executed

printf(“%d\n”,arr[i]); => 5

2.Two dimensional array

Two dimensional array defined as array which stored in tabular form.

In two dimensional array we specify number of rows and column.

Syntax :-. <datatype> array name[rows][cols];

Two dimensional array initialization

int arr[2][2]={{1,2},{3,4}};

Accessing two dimensional array

Arr[0][0]=1;

Arr[0][0]=2;

Arr[0][0]=3;

Arr[0][0]=4;

Example:- int a[5][5];

Two dimensional array
Two dimensional array

Two dimensional array program in c

12345678910111213141516171819202122232425#include <stdio.h>int main(){    int i,j;int a[3][3]={ {1,2,3},{4,5,6},{7,8,9} };printf(“Two dimensinonal array \n”);for (i=0; i<3;i++){ for(j=0; j<3; j++) {printf (“%d\t”,a[i][j]); } printf(“\n”); }}

Output :-

Two dimensional array
Two dimensional array

3. Multidimensional array

Cpp programming language allows programmer to create arrays of arrays known as multidimensional arrays.

Example :  

int arr[2][4][3];

arrays in c

arrays in c

Filed Under: C Tagged With: Array declaration in cArray DefinitionInitialization of array

Leave a Comment

Your email address will not be published.