Array in C Language

Suppose you have a task to store 100 numbers, then what you will do. You will declare 100 variables and 100 input statements to store values. Instead of this, there is another option, declare an Array of 100 sizes. An array is a collection of more than one elements under one variable name.

In this tutorial, we will learn about array in c programming. What is array, how to use an array in c language, how to store and traverse array with the example?

Some Points About Array.

  • The array is a collection of data items.
  • It stores the same type of data.
  • We can declare any type of array.
  • Each element identified by an index, known as the key.
  • Array index or key always start with 0.
  • An array can implement as Stack, Queue and other data structure.

Types of Array in C

  • Single dimensional array
  • Double dimensional array

Single Dimensional Array in C:

The single dimensional array is used to store data in a linear format. You can think array as a row with a single name but different index number. Indexes are start with zero and values are stored one after another.

data_type array_name[size];

example:

int n[6];	//integer type Array
float n[6]	//float type array
char n[6];	//char array in C
Single Dimensional Array Example

In the above picture, we have declared an array to store 6 values. Address of the first cell is n[0] and the last cell is n[5]. Below c program is an example to store and print values of an array in c language.

Example 1: Array example in C language.

#include<stdio.h>
int main()
{
//declaring an array to store 3 integer values.
int n[3];
	//accept user input in an array
	printf("\nEnter 3 numbers : ");
	scanf("%d%d%d",&n[0],&n[1],&n[2]);
	
	//printing values of array
	printf("\nFirst Value n[0] : %d",n[0]);
	printf("\nSecond Value n[1] : %d",n[1]);
	printf("\nThird Value n[2] : %d",n[2]);
return 0;
}
Sample Output:
Enter 3 number : 10 20 30

First Value n[0] : 10
Second Value n[1] : 20
Third Value n[2] : 30

Example 2: C program to accept 10 user input in the array and print the sum of given numbers.

#include<stdio.h>
int main()
{
int n[10];
int i,sum=0;
	for(i=0;i<10;i++)
	{
		printf("\nEnter a number");
		scanf("%d",&n[i]);
		sum=sum+n[i];
	}
	//printing values of array
	for(i=0;i<10;i++)
	{
		printf("\nValue : %d",n[i]);
	}
	printf("\nSum of numbers : %d",sum);
return 0;
}

In the above program, the variable i is used to generating the index number of an array using the loop. When the value of i is 0, at that time n[i] is represented as n[0] and so on.

Double Dimensional Array

Double dimensional Array is also known as a matrix. Double dimensional array in C is represented as a table, that is a combination of rows and columns. Index of rows and columns in the matrix always starts with 0.

data_type array_name[rows][column]

int n[3][3]; 
float n[3][3];
Double Dimensional Array in C

Example 3: Double dimensional array example in c language.

#include<stdio.h>
int main()
{
int n[3][3];
int i,k;
//accepting user input in matrix
for(i=0;i<3;i++)
{
	for(k=0;k<3;k++)
	{
		printf("Enter a number");
		scanf("%d",&n[i][k]);
	}
}
//printing array in matrix format
for(i=0;i<3;i++)
{
	for(k=0;k<3;k++)
	{
		printf("%d ",n[i][k]);
	}
printf("\n");
}

return 0;
}