C Programming 1D Arrays
C Programming 1D Arrays
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the
user want to store marks of 100 students. This can be done by creating 100 variable individually but, this
process is rather tedious and impracticable. These type of problem can be handled in C programming using
arrays.
An array is a sequence of data item of homogeneous value(same type).
Arrays are of two types:
1. One-dimensional arrays
2. Multidimensional arrays( will be discussed in next chapter )
For example:
int age[5];
Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All element
in an array are of the same type (int, in this case).
Array elements
Size of array defines the number of elements in an array. Each element of array can be accessed and used by
user according to the need of program. For example:
int age[5];
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an array.
scanf("%d",&age[i]);
/* Statement to insert value in (i+1)th element of array age[]. */
/* Because, the first element of array is age[0], second is age[1], ith is age[i -1]
and (i+1)th is age[i]. */
printf("%d",age[0]);
/* statement to print first element of an array. */
printf("%d",age[i]);
/* statement to print (i+1)th element of an array. */
Output