Arrays in C
An array in C is a collection of items stored at common border memory locations and elements can be accessed randomly using indices of an array.
They store only similar type of elements. They are used to store the common data types like, int, float etc. An array in C can store derived data types such as the structures, pointers etc.
Declaring Array in C
To declare an array in C, you need to specify the type of the elements and the number of elements required by the array.
Syntax for Declaring an Array
type arrayName [ arraySize ];
Initializing an Array
For initializing an array, you need to store the elements in the array.
Syntax for Initialising an Array
type arrayName [ arraySize ] = {data/elements};
Accessing Array Elements
An element is accessed by indexing the array name. The required value is called by its index in [ ].
Syntax for Accessing an Array
new_array= arrayName [index];
Sample program for Array
#include<stdio.h>
int main()
{
int i=0;
int marks[5];
marks[0]=90;
marks[1]=70;
marks[2]=80;
marks[3]=75;
marks[4]=65;
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}
Output
90
70
80
75
65