Array

Array is collection of variables having the same data type. Suppose, you need to read marks of 10 students and print them. For this problem, you can declare 10 different variables and can solve the marks of the students in that. But if you are using Array to solve this problem, you can do it very easily then previous solution. Here, we have to declare an array which have the ability to store the marks of students which is having the same data type i.e. integer. 

How to declare an array?

datatype arrayName[size];
Example : int marks[5];

To declare an array, you need to follow the above syntax. You can create an array of different data types such as int, char, etc.

Array of data type "char" is Strings. 

How to store data in an array?

You have to run a loop to store data in array, as everytime you need to increment the array index. So, let's learn how to store data in an array.

for(i=0;i<=sizeofarray;i++)
{
 scanf("%d", &arrayName[i]);
}

NOTE : In old compilers, you need to declare the size of the array i.e. keep the size as constant and it can't be variable but in new compilers you can keep the size of an array as variable.

How to print the stored value from the array?

printf("%d", arrayName[0]);

To print the stored value from the array, follow the above syntax, if the array is of Integer data type. This will print the first element of the array. 

NOTE : Array indexing starts from '0' and not from '1'.
Previous
Next Post »
0 Comment