Array Declaration
To declare an array we need three things as:
- What kind types(i.e. data type) of array?
- What is the name of array?
- What is the size of array?
In our C program, we have declare array as:
syntax: data_type array_name[SIZE];
example: int result[10];
Here, int is a data type and the word result is the name of array. The [10] is however is new. The number 10 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The bracket ( [] ) tells the compiler that we are dealing with an array.
Accessing element of an Array
Once an array is declared, we can access it and use it in our program.
Now you know that, all the array elements are numbered, starting with 0. Thus, result[10] is not the 10th elements of the array, but the 9th.
We are using 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 in turn. This ability to use variables to represent subscripts is what makes arrays so useful.
Entering Data into an Array
int result[10];
for(i=0; i<10; i++)
{
printf("\nEnter elements: ");
scanf("%d", &result[i]);
}