Introduction
Arrays in c programming is a very important concept. C language allows us to create a group of similar type of data and this group is called arrays. In this article, I will explain what are arrays in c, how to declare and initialize it, memory organization of the array, how to access arrays and types of arrays. In short, you can learn how to create arrays and how to manipulate in c.
Why arrays are used in C?
Before knowing this, let’s imagine you need to read, store and display salary of 100 employees. Then what will you do?
- We will have to declare 100 variables of type int or float.
- After declaring variables, one message will be displayed to the user to enter a salary of 100 employees.
- For reading this information, you will have to use 100 format specifiers and 100 parameters in the scanf() statement.
- Now, you have to display the same information. Then, in the printf() statement, again you will have to write 100 format specifiers and 100 parameters.
- Now, your program is over.
Can you imagine, how much time you will require for writing this program? If you are not getting what I am trying to say, then please write this program for me. Okay. I am writing this same program for you but, instead of 100 employees, I will take only 10 employees. Because it will take a lot of time.
Also read: C Program to Find the Sum of Cubes of Elements in an Array
C Program for salary of 10 employees without using Arrays in C
#include<stdio.h>
int main()
{
int e1,e2,e3,e4,e5,e6,e7,e8,e9,e10;
printf("Enter salary for 10 employees\n");
scanf("%d%d%d%d%d%d%d%d%d%d",&e1,&e2,&e3,&e4,&e5,&e6,&e7,&e8,&e9,&e10);
printf("Salary Details for 10 employees\n");
printf("%d%d%d%d%d%d%d%d%d%d",e1,e2,e3,e4,e5,e6,e7,e8,e9,e10);
return 0;
}
I hope you have understood what I was trying to say. As you can see, we are doing the same thing again and again. To avoid this, we have the concept arrays in c programming. Now, I am writing the same program using arrays. But, instead of 10 employees, I am using 100.
C Program for salary of 100 employees using Arrays
#include<stdio.h>
int main()
{
int e[100],i;
printf("Enter salary for 100 employees\n");
for(i=0;i<100;i++)
{
scanf("%d",&e[i]);
}
printf("Salary Details for 100 employees\n");
for(i=0;i<100;i++)
{
scanf("%d\n",&e[i]);
}
return 0;
}
Just compare the above two programs. You can see the difference. The second program is for 100 employees but I have declared only a single variable i.e. array variable.
What are arrays in C?
An array is nothing but a variable where we can store a similar type of data. When we create an array in c then contiguous memory locations for that array variable.
How to declare an Array
Declaring arrays in c programming is as simple as declaring a normal variable. That means, just like normal variable, we can declare an array. See the following syntax.
data-type array_name[size];
Here, data_type is any primary data type like int, float, char etc, array_name is the name of the array and size is the size of the array. See the following examples.
int num[100]; // array of 100 integers.
float marks[300]; // array of 300 floats.
char name[50]; // array of 50 characters.
I hope you have understood how to declare arrays in c programming. If you want to read the salary of 100 employees, then there is no need to declare 100 variables. Instead, you can directly use arrays. But, for effectively using an array, we have to use loops in c programming.
Now, what happens when we declare an array? The first example, int num[100] creates 100 memory locations and the size of each location is 4 bytes. So, 400 bytes are allocated for this array. For calculating the total size of the array, you must know the size of data-type you are using.
Initializing Arrays in C
There are two ways to initialize an array in c programming. One is at compile time and another is run-time. What is the difference between these two? At compile time means at the time of program creation. Every time, we run the program, these arrays values will not be changed. At run-time means we will read values from the console.
int a[]={3,7,12,1,8};
The above statement shows declaration as well as initialization of an array. That means we are declaring and initializing an array at the same time. So, there is no need to specify the size of the array. If you still want to give the size, then there will not be any error.
Always remember, arrays always start from index 0. See the following memory organization for the above example.

As you can in the above figure, there are two statements. First is the declaration and other is a declaration with initialization. Here, from 1000 to 1016 are addresses.
How to Access an Array Elements
After declaring an array, we need to access array elements for performing certain operations. This can be done with the subscript, the number in brackets following the array name. The number is nothing but a position of an element in an array. As I said above, the array index starts from 0. That means a[0]= 3, a[1]=7, a[2]=12 and so on. In the given array, 3 is at first position but while accessing this position, we will have to use a[0].
C Program to read 10 elements and display the same using Arrays
#include<stdio.h>
int main()
{
int a[10],i;
printf("Enter 10 elements\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("You have entered the following elements\n");
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Output of the above program
Enter 10 elements
6 5 9 88 3 23 11 65 34 10
You have entered the following elements
6 5 9 88 3 23 11 65 34 10
From the above program, we have done two things. First, we have read the array elements from the user i.e. run-time initialization of arrays in c programming. Second, we have displayed the array elements to the output screen.
We can read or display array elements using for loops. Without the loop, if you try to read or write array elements then there will not be any use of the array.
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
The above for loop will be executed 10 times that means scanf(“%d”, &a[i]); will also be executed 10 times. When the value of i=0, then the first element will be stored at the address of a[i] i.e. a[0]. As the value of i changes with each iteration from 0 to 9, then each and every element will be stored at the respective address of a[0] to a[9]. In this way, you can read thousand of values using arrays in c programming.
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
When we want to display array elements, then we can use the above code. Here, the value of i decides the position of the array element. When i=0, then the value of a[0] i.e. 6 will be displayed on the screen.
How do you find the size of an array in C?
There is a special operator in c programming i.e. sizeof(). By using this operator, you can find size of any variable of any data-type. So, we know, array is also a variable where we can store same type of data. See the following program.
C Program to find size of an array in c programming.
#include<stdio.h>
int main()
{
int arr[]={2, 4, 8, 3, 21};
printf("Size of the array arr[] is %d", sizeof(arr));
}
The above printf will print Size of the array arr[ ] is 20. Why 20? Because there 5 integer elements in an array and each integer takes 4 bytes (in case 2 bytes then size is 10).
What are the types of arrays in C?
Basically, there are two types of arrays.
- One Dimensional Array i.e. which have the only subscript. Eg. int a[10].
- Multi-Dimensional Array i.e. which have more than one subscript. If an array has two subscripts, then it is known as a two-dimensional array. For example, int a[5][5]
In this article, I have explained about only one dimensional array.
Important points to Remember for Arrays in C Programming
- Array is nothing but a collection of similar elements.
- First position of an element in the array is always start with 0 and last position of an element is 1 less than the size of the array.
- Like a normal variable, array variable is also declared before using.
- Array elements are always stored in contiguous memory location.
- Array elements can be initialized at compile time and runtime.
- You can access any elements from the array just by knowing its position.
- For the better use of array, you must have to use loops in c programming.
Arrays in c programming is a very vast topic. I have tried to explain this in very easy language. If you have any problem regarding this, please, feel free to contact me. Please watch the following video for a better understanding of arrays in c.
Thanks for reading and watching.