C arrays are an essential data structure that allows you to store multiple elements of the same data type in a sequential manner. In single dimensional arrays the data is stored in one dimension that is one row. The single dimensional arrays are initialized by specifying the name of the variable followed by squire brackets. Inside squire brackets we have to specify the integer, which is interpreted as size of the array. The syntax as follows.
datatype varialblename[size]// Size must be integer
Example:
int numbers[20]// number is an integer array and its size is 20
float gdps[10]// gdps is an float arrays and its size is 10
The values can be initialized to an array by index of the array. Generally the array index starts with 0,
ends with the size-1. If we want to initialize the value then we have to specify the array name followed by index value
in squire brackets.
Array can be initialized by specifying the index in the squire brackets. If we want
to initialize first element then we have to specify as follows
marks[0]=20;
The above statement can be interpreted as first element of marks array is initilizes with 20;
Similarly if we want access or display we have to specify the index of the array
which we want to access. Example
printf("%d", marks[2]);
The above print statement prints third element of marks array.
Always the array index starts with 0
If we want to initialize array elements in run time we can use scanf function similar to
normal variable initialization. But in the scanf function we have to refer the
arrayname along with the row index and column index in the squire brackets. The following statement
illustrates the initialization of array element in run time.
scanf("%d",marsk[2]);
The above scanf statement initializes marks array third element with a values which will
be entered in run time.
If we want to initialize all the elements of the array at a time in run time then we will use scanf
function in any loop with appropriate loop condition. For example marks array is declared with 3
then the scanf statement in the loop is as follows
for(i=0;i<3;i++){ //i for number of elements
scanf("%d",marks[i]);
}
Similarly if we want display all the elements we can use the following snippet.
for(i=0;i<3;i++){ //i for number of elements
printf("%d",marks[i]);
}