Array Subscript in C is an integer type constant or variable name whose value ranges from 0 to SIZE 1 where SIZE is the total number of elements in the array. Let us now see how we can refer to individual elements of an array of size 5:
Consider the following declarations:
char country[ ] = “India”; int stud[ ] = {1, 2, 3, 4, 5};
Here both arrays are of size 5. This is because the country is a char array and initialized by a string constant “India” and every string constant is terminated by a null character ‘\0’. And stud is an integer array. country array occupies 5 bytes of memory space whereas stud occupies size of 10 bytes of memory space.
The following table shows how individual array elements of country and stud arrays can be referred:
Element no. | Subscript | country array | stud array | ||
Reference | Value | Reference | Value | ||
1 | 0 | country [0] | ‘I’ | stud [0] | 1 |
2 | 1 | country [1] | ‘N’ | stud [1] | 2 |
3 | 2 | country [2] | ‘D’ | stud [2] | 3 |
4 | 3 | country [3] | ‘I’ | stud [2] | 4 |
5 | 4 | country [4] | ‘A’ | stud [4] | 5 |
Reference of individual elements
A program to illustrate how the marks of 10 students are read in an array and then used to find the maximum marks obtained by a student in the class.
/* Program to find the maximum marks among the marks of 10 students*/ # include < stdio.h > # define SIZE 10 /* SIZE is a symbolic constant */ main ( ) { int i = 0; int max = 0; int stud_marks[SIZE]; /* array declaration */ /* enter the values of the elements */ for( i = 0;i<SIZE;i++) { printf (“Student no. =%d”,i+1); printf(“ Enter the marks out of 50:”); scanf(“%d”,&amp;stud_marks[i]); } /* find maximum */ for (i=0;i<SIZE;i ++) { if (stud_marks[i]>max) max = stud_marks[ i ]; } printf(“\n\nThe maximum of the marks obtained among all the 10 students is: %d ”,max); }
OUTPUT
Student no. = 1 Enter the marks out of 50: 10
Student no. = 2 Enter the marks out of 50: 17
Student no. = 3 Enter the marks out of 50: 23
Student no. = 4 Enter the marks out of 50: 40
Student no. = 5 Enter the marks out of 50: 49
Student no. = 6 Enter the marks out of 50: 34
Student no. = 7 Enter the marks out of 50: 37
Student no. = 8 Enter the marks out of 50: 16
Student no. = 9 Enter the marks out of 50: 08
Student no. = 10 Enter the marks out of 50: 37
The maximum of the marks obtained among all the 10 students is: 49