Introduction To Array
Introduction To Array
UNIT V
ARRAYS AND FUNCTIONS
ARRAYS
▪ What is an Array
▪ Why we need an Array in C programming
▪ How to declare an Array in C
▪ How to access elements of an Array
▪ How to initialize an Array
▪ Consider a scenario where you need to find out the average of 100 integer
numbers entered by user.
▪ In C, you have two ways to do this: 1) Define 100 variables with int data type
and then perform 100 scanf() operations to store the entered values in the
variables and then at last calculate the average of them
▪ 2) Have a single integer array to store all the values, loop the array to store all
the entered values in array and later calculate the average.
▪ Which solution is better according to you? Obviously the second solution, it
is convenient to store same data types in one single variable and later access
them using array index
To declare an array in C, you have to specify the type of the elements and the number
of elements required by an array as follows −
Here, we declared an array, data, of integer type. And its size is 100. Meaning, it can
hold 100 integer values.
It is important to note that the size and type of an array cannot be changed
once it is declared.
Now if we want to make the value of the third element to 12, then we will write
mark[2] = 12;
❖Array can store a fixed-size sequential elements of the same data type.