A variable can hold only one literal value at a time, If you want to store 100 different values then you have to declare 100 different variables which is very difficult. To overcome this problem, C Language introduced the concept of an arrays.
An array can be defined as an collection of homogeneous (similar type) elements. For example, an integer array holds the elements of int types, a float array hold the elements of float types etc.
Some important points about C Arrays:
Arrays can be categorized into 2 categories:
A one-dimensional array is one directional array, which contain only one single row and multiple columns. One-dimensional array can be declared as follows:
data_type array_name[array_size]; int arr[5];
Example 1: WAP to input 10 numbers in 1D-Array and print them. 1
#include<stdio.h>
void main()
{
int a[10], i;
printf("Enter 10 numbers: ");
for(i=0;i<=9;i++)
{
scanf("%d", &a[i]);
}
printf("\nInput numbers are:\n");
for(i=0;i<=9;i++)
{
printf("%d ",a[i]); //display array values
}
}
Array values can be input from user or can be declared directly.
int a[5] = {10,20,30,40,50}; //Declaration with Initialization int a[] = {20,30,40,50,60}; //can define without specifing the size.
Example 2: WAP to initialize array elements. 1
#include<stdio.h>
void main()
{
int i;
int a[5] = {10,20,30,40,50}; //Declaration with Initialization
printf("Array list is:\n");
for(i=0;i<=4;i++)
{
printf("%d ", a[i]);
}
}