Array in C Programming : How to Declare , Read Write with Example

Arrays in C programming ?

An array is a collection of data items whose data type is same. Basically array is collection of variables whose data type is same. the collection has a common name, called as array name.

Elements of an array i.e individual variables are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name. Array index must be of integer type either int, long int or char type only.

How to Declare Array

Here, subject is array of 5 elements i.e variables of integer data type, Means, it can hold 5 integer type values.

Note that index number is always starts from zero. means first array element or variable is stored at 0th position and second at 1st position and so on. Hence the position of last array element is always (array_size – 1).

There are two type of array : Single-Dimensional & Multi-Dimensional Array.

int SUBJECTS [5] ;

single-dimenstion array
single-dimension array





One-dimensional array is like a list with fixed size. In example above, subject is array name having 5 elements of integer type. Means its a collection of variables of integer type. Every variable in the list is identified by index. So first variable is identified as subjects[0] , second as subjects[1] & so on. the last variable is always at subjects[size-1] position.

How to Read Array ?

To read array elements, we can use any looping statements like for loop , while loop or do while loop. But the for loop is easier to use for array related programs.


In above program C-Program-Array-Read-Write.c (81 downloads) , subject_marks is array of 5 elements whose data type is integer. for loop is used to iterate in array , index is variable for iterations in array.

for index=0 , input value in first variable of array as subjects_marks[0] which is 78

for index=1 , input value in second variable of array as subjects_marks[1] which is 85

for index=2 , input value in third variable of array as subjects_marks[2] which is 92

for index=3 , input value in fourth variable of array as subjects_marks[3] which is 91

for index=4 , input value in fifth variable of array as subjects_marks[4] which is 87

Same way, Here another for loop is used to display the values from this subject_marks array variable.















Lets see,

How to find Total Marks and its Average.

To find total marks , we have to sum values of array elements, divide it by number subjects to get average marks.

Here initial value of total variable is 0,

for first iteration, value of total variable is 0+ value of first array element, which is 0+ 78 = 78

for second iteration, value of total variable is 78+ value of second array element, which is 78+ 85 = 163

for third iteration, value of total variable is 163 + value of third array element, which is 163+ 92 = 255

for forth iteration, value of total variable is 255 + value of fourth array element, which is 255+ 91 = 346

for fifth iteration, value of total variable is 346 + value of fifth array element, which is 346+ 87 = 433

so finally total variable holds 433 which is total marks. Here size of array is 5 which is the number of subjects. Hence the value of average variable is total/ 5 , which is 86.6. Note that, In C Programming division of two integers is in integer format. To get decimals , either numerator or denominator should be in float format. so typecast numerator by float as the instruction became float(total)/5



C Programming String Read Write Example