Array in C

Array in C

Array is the variable that can store multiple values of similar data types or collection of multiple values of similar data type stored in contiguous memory location.

Every data item of an array is called an element. And each element is unique and stored  in separated memory location.

For example if we want to manage salary record of 200 employee. in this case we have two options

  1.   Declare 200 variables to store the salary record of 200 employee i.e one variable store salary amount of one employee.

  2.   Declare one variable which hold all the 200 values.

Obviously the second option is better, that would be much easier to handle one variable than the 200 different variables. This is possible because single variable can store multiple values of similar data types are in contiguous memory location by using array.


 Each of elements of an array share single variable but  when stored at memory location each element having different index no. known as subscript. The number of subscript is always starts with zero. So when we store the values in one dimensional array is known as vector.

Declaration of an Array

syntax is :

Data type array name [size];

 When we declare an array it tells the compiler that, the data type, name of the array, size of the array and for each element it allocate separate memory space.

Different data type will occupy different memory space for each element 

int data type occupies 2 bytes

float data type occupies 4 bytes for each element

And once size of array is declared it can not be changed.

We can represent individual array as :

int mark[5];

here we have array with name  mark of size 5 element and is having int data type

we can also declare  specify the size of the array as symbolic constant:

#define SIZE 10;

Initialization of an Array:

When we declare  element of local array has garbage value. If it is global  array or static array then it will be by default initialize with zero. An explicitly it can be initialize that

Data_type array_name [array size ] = {value1, value2, value3…}

or

Data_type array_name [ ] = {value1, value2, value3}

 Example:

int arr[7]={10,24,56,85,96,75,14}

int mark[ ]={45,56,58,54,65}

in second example we have not declared size of array but compiler will identify size is 5 as we are initialized with 5 elements.  

 Total size in byte for 1D array is

Total bytes = (size of data type )* (size of array)

Example : if an array declared is:

int [15];

Total byte= 2 * 15 =30 byte.

 Accessing of Array Element

/* program to add values into an array and display them*/

#include<stdio.h>

int main()

{

    int arr[7],i;

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

  {

     printf(“Enter a value for arr[%d] \n”,i);

     scanf(“%d”,&arr[i]);

   }

printf(“The array elements are: \n”);

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

  {

   printf(“%d\t”,arr[i]);

   }

return 0;

}

In the program the using below code we placed data into array

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

  {

     printf(“Enter a value for arr[%d] \n”,i);

     scanf(“%d”,&arr[i]);

   }

For loop is used to take data from user and store in array , first time loop will start with loop , i has value 0 so by using scanf() function the value will be stored at arr[0], the first element of array and loop will repeatedly execute until i become 6. So the last element in the array is arr[6].

In scanf() function we have used  (&) operator on element arr[i] of the array as &arr[i] so we are just passing the address of array element to the scanf() function instead of its value.

Reading data from an array

In above example below code we used to read  stored data of array and print as output

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

  {

   printf(“%d\t”,arr[i]);

   }

 For loop is much  the same but now the body of loop cause to print stored array, first time loop will start with loop , i has value 0 so by using printf() function the value will be read  the value stored at arr[0]and print to out put window, the first element of array and loop will repeatedly execute until i become 6. So the last element in the array is arr[6].

For above example we get following output.

 Enter a value for arr[0]  13

Enter a value for arr[1]  23

Enter a value for arr[2]  56

Enter a value for arr[3]  27

Enter a value for arr[4]  44

Enter a value for arr[5]  48

Enter a value for arr[6]  99

The array elements are: 13 23 56 27 44 48 99

  In this tutorial we have just explored about introduction of array with one dimension array we also have two or more dimensions of arrays. The two dimensional array is also known as matrix.

 

Previous
Next Post »