Overview:
  • C stores lists of values in arrays. An array is a group of related memory locations. These locations are related by the fact that they all have the same name and the same type. To refer to a particular location or element within the array, we specify the name of the array and the subscript.
  • A subscript may be an integer or an integer expression. If a program uses an expression as a subscript, then the expression is evaluated to determine the particular element of the array.
  • It is important to note the difference when referring to the seventh element of the array as opposed to array element seven. The seventh element has a subscript of 6, while array element seven has a subscript of 7(actually the eighth element of the array). This is a source of “off-by-one” errors.
  • The elements of an array can be initialized in a definition, in assignment statements and by inputting the values directly into the elements of the array.
  • If there are fewer initializes than elements in the array, C initializes the remaining elements to zero.
  • C does not prevent referencing elements beyond the bounds of an array.
  • Apply static to a local array definition so the array is not created each time the function is called and the array is not destroyed each time the function exits.
  • Arrays that are static are automatically initialized once at compile time. If the programmer does not explicitly initialize a static array, it is initialized to zero by the compiler.
  • To pass an array to a function, the name of the array is passed. To pass a single element of an array to a function, simply pass the name of the array followed by the subscript (contained in square brackets) of the particular element.
  • C passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays.
  • To receive an array argument, the function’s parameter list must specify that an array will be received. The size of the array is not required (for single-subscripted arrays) in the array brackets.
  • C provides the special type qualifier const to prevent modification of array values in a function. When an array parameter is preceded by the const qualifier, the elements of the array become constant in the function body and any attempt to modify an element of the array in the function body results in a compile time error.
  • When a function receives a single-subscripted array as an argument, the array brackets are empty in the function’s parameter list. The first subscript of a multiple-subscripted array is not required either, but all subsequent subscripts are required. The compiler uses these subscripts to determine the locations in memory of elements in multiple subscripted arrays.
  • To pass one row of a double-subscripted array to a function that receives a singlesubscripted array, simply pass the name of the array followed by the subscript (in square brackets) of that row.

void main()
{
int x;
x=5;
x=10;
printf(“x=%d”,x);
}

When the above program is executed, 10 is printed. This is because, when the value 10 is assigned to x, the old value of x is lost. Thus, ordinary variables are capable of holding only one value at a time. However, there are situations in which we would want to store more than one value at a time in a single variable. For example, arranging the aggregate marks obtained by 100 students in ascending order. In such a case we have two options to store these marks in memory.
a) Construct 100 variables to store aggregate marks obtained by 100 different students i.e., each variable contains one student marks.
b) Construct one variable called array capable of storing or holding all the hundred values.

Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables. Moreover, there are certain logics which cannot be dealt with, without the use of an array.

  • Array is a collection of same type elements under the same identifier referenced by index number.
  • Arrays are widely used within programming for different purposes such as sorting, searching and etc.
  • Arrays are of two types single dimension array and multi-dimension array.
  • Each of this array type can be of either static array or dynamic array.
  • Static arrays have their sizes declared from the start and the size cannot be changed after declaration.
  • Dynamic arrays that allow you to dynamically change their size at runtime, but they require more advanced techniques such as pointers and memory allocation.

Declaration of an Array
Arrays must be declared before they can be used in the program. Standard array declaration
is as,
Syntax:
Data_type arr_var_name [size];

For example
int myArray[5];

Here int specifies the type of the variable and the word myArray specifies the name of the variable. The number 5 tells the number of elements of type int that will be in the array and is called the dimension of the array.

Initializing Arrays

Initializing of array is very simple in c programming. The initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name.
Here is an example which declares and initializes an array of five elements of type int.
int myArray [5] = {1, 2, 3, 4, 5};

Array can also be initialized after declaration.
int studentAge[4];
studentAge[0]=14;
studentAge[1]=13;
studentAge[2]=15;
studentAge[3]=16;

 

Array in c language

Accessing elements of an array:

Once an array is declared, the individual elements in the array can be referred with subscript, the number in the brackets following the array name. This number specifies the element’s position in the array. All the array elements are numbered, starting with 0. Thus myArray[2] is not the second element of the array but the third. In the above declaration, we use the variable i as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array. This ability to use variables as subscripts marks arrays very useful.

Read and write the data of an array:
#include <stdio.h>
#include <conio.h>
void main(){
int arr[5];
int i;
clrscr();
printf(“enter 5 elements into array :\n”);
for(i = 0;i<5;i++){
printf(“enter element %d :”,i+1);
scanf(“%d”,&arr[i]);
}
printf(“the array elements are :\n”);
for(i = 0;i<5;i++){
printf(“%d\n”,arr[i]);
}
}

Note: The elements of an array can be initialized in a definition, in assignment statements and by inputting the values directly into the elements of the array. If there are fewer initializes than elements in the array, C initializes the remaining elements to zero.

Write a program to find largest element of an array.

#include <stdio.h>
int main(){
int i;
float arr[10],large;
printf(“Enter 10 elements to find largest among them: \n”);
for(i=0;i<10;++i)
{
printf(“Enter Number %d: “,i+1);
scanf(“%f”,&arr[i]);
}
large=arr[0];
for(i=1;i<10;++i)
{
if(large<arr[i])
large=arr[i];
}
printf(“Largest element = %.2f”,large);
return 0;
}

Like integers, characters etc., we can also pass arrays as an argument to a function.

#include<stdio.h>
#include<conio.h>
void main()
{
void read(int *,int);
void display(int *,int);
int a[5],i,sum=0;
clrscr();
printf(“Enter five elements for an array \n”);
read(a,5);
printf(“The list elements are \n”);
display(a,5);
for(i=0;i<5;i++)
{
sum+=a[i];
}
printf(“The sum of the elements of the array is %d\n”,sum);
getch();
}
void read(int c[],int i)
{
int j;
for(j=0;j<i;j++)
scanf(“%d”,&c[j]);
fflush(stdin);
}
void display(int d[],int i)
{
int j;
for(j=0;j<i;j++)
printf(“%d “,d[j]);
printf(“\n”);
}


Merging of two array elements:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],m,n,i,j;
printf(“enter the size of array a(<=10):”);
scanf(“%d”,&m);
printf(“enter the %d elements of a:”,m);
for(i=0;i<m;i++)
scanf(“%d”,&a[i]);
printf(“enter the size of array b(<=10):”);
scanf(“%d”,&n);
printf(“enter the %d elements of b:”,n);
for(i=0;i<n;i++)
scanf(“%d”,&b[i]);
for(i=0;i<m;i++)
{
c[i]=a[i];
}
for(j=0;j<n;j++)
{
c[i+j]=b[j];
}
printf(“the merged elements are:”);
for(i=0;i<m+n;i++)
printf(“%d\n”,c[i]);
getch();
}

Write a C program to sort the array of 10 elements in descending order.

#include <stdio.h>
void Descend(int c[]);
int main(){
int c[10],i;
for(i=0;i<10;++i){
printf(“Enter element %d:”,i+1);
scanf(“%d”,&c[i]);
}
Descend(c);
printf(“In descending order: “);
for(i=0;i<10;++i){
printf(“%d\t”,c[i]);
}
return 0;
}
Descend(int c[]){
int i,j,temp;
for(i=0;i<9;++i)
for(j=i+1;j<10;++j)
{
if(c[i]<c[j]){
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}

Scroll to top