C - Arrays


An array is an collection of data element of similar data datype.What does even that actually mean.

To understand Array you must know what is subscript

SUBSCRIPT :

  • A subsript is an integer variable/constant whose value referes to an individual array element. In other word , it is the subscript which distinguish the individual array elements from one another.
  • If a one dimensional array contains n-elements, the subscript will be an integer quantity ranging from 0 to n-1.
  • Suppose that x is a 10 element array. the first element is referred t o as x[0],the second as x[1] and so on . The last element will be x[9]. The subscript associated with with each element is shown in the square brackets. Thus the subscript for the first element is 0, for second it is 1 and so on.
  • A subscript can also be a variable . Hence x[0] can also be referred to as x[i] where i=0.

DECLARING AN ARRAY:

  • Array are defined in much the same manner as ordinary variable , except that each array name nust be accompanied by a specification(i.e. the maximum number of elements).
  • For a one dimensional array , the size is enclosed in square brackets size is usually written positive integer constant and sometimes a symbolic constant
  • In general terms , a one dimensional array definition may be expressed as
    storage-class data-type array-name[size] ;
  • or example the declaration int x[10]; will create an array of 10 integers. The same can be achieved using the following statement:
    #define size 10
    int x[size];
    where size is symbolic constant having a value of 10.

INTIALISATION OF AN ARRAY:

I)We can directly write the values of data elements of array inside curly braces.
eg. int a[5]={0,1,2,3,4};
char c[5]={a,e,i,o,u};

II)we may intialize any position of array
int a[5];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;

III)Generally we ask user to put the elements in array.In this method we first ask user to give the size of array and then tell to put elements in array;
eg.
printf("Enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);


  • Print Page