Array in C
Arrays in C are also known as sub scripted variables. Array is basically a collection of similar items. Same thing apply to array in C.
Array in C |
When in some C language program there is need of using a large number of variables in a single program, it become tedious for programmer to deal with such a large list of variables.
For example, if we have to read roll nos of 100 students of a class in a program, we have to use 100 different variables in a single program.
Declaring 100 different variables and then reading them with 100 format strings is difficult. To overcome this problem, we can use Array in C language.
What is an array
Arrays are basically collection of variables of same data type.
Types of Array In C
There are two types of arrays in C
- One Dimensional Array
- Two Dimensional Array
One Dimensional Array
One Dimensional Array in C is like a list of items arranged in a row or in a column. For example , consider the list- 10, 20,34, 56, 78, 2 . You can arrange elements of a one dimensional array as a row or column of elements. One dimensional arrays are usually processed using a for loop.
Array in C starts with index 0. Suppose you have array A of size 10, the first element will be A[0]
How to declare One Dimensional Array
The general syntax for declaring One Dimensional Array in C
datatype name of array[size];
Here datatype is the datatype of elements to be stored in array. Name of the array is the name you wants to assign to array in C .
Square brackets([ ]) indicates that this is an array.
Size is the number of elements array will store. Size must be integer.
Example
int A[10];
Here name of the array is A, size is 10, all the ten elements of the array will be integers.
Similarly you can declare array of float data types and char data types.
float b[10];
char c[5];
Programming Examples of One Dimensional Arrays
Program to read 10 elements and print these elements.
main()
{
int a[10],i;
printf("Enter Elements ofthe list\n");
for(i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}
printf("You have entered following list");
for(i=0;i<=9;i++)
{
printf("\n%d",a[i]);
}
}
{
int a[10],i;
printf("Enter Elements ofthe list\n");
for(i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}
printf("You have entered following list");
for(i=0;i<=9;i++)
{
printf("\n%d",a[i]);
}
}
Program to sort a list of elements.
Program to search an element in a list of elements.
Two Dimensional Array in C
Two Dimensional Array in C are the arrays , where elements are arranged in the form of matrix. Each element of a Two Dimensional Array in C will have a row number and column number.
Array in C is usually processed in loops, for loop is most convenient loop for array processing.
Two Dimensional Arrays are to be processed in nested loops.
Programming examples of two Dimensional Arrays
Program to print transpose of a matrix
Program to Add Two Matrices
Program to Multiply Two Matrices
Relational Operators of C language
C language operators
Popular Programs of C language
Program to Reverse a Number in C language
Program to Calculate Factorial of a number in C language
Program to print Fibbonacci sequence in C language