What is an Array in C
What is an Array in C
1 4 8 25 2 17
The elements in this array are 1, 4, 8, 25, 2, and 17, and they are stored at
indexes starting from 0 to 5 (size-1). However, it is essential to declare the
data types of the elements stored in the array while declaring. The basic
syntax for declaring an array in C is given here.
Let us name our array “ arr” and declare it using the above syntax. The
data type of our array elements will be integer, and the size is 6.
1 4 8 25 2 17
Page |1
In C language, we can declare an array by first naming the data_type of the
items stored in the array. Keep in mind that the data types of all the elements
inside the array need to be similar. Now, name your array using
the naming guidelines for C with the size of the array. When we declare an
array in C, our compiler will continuously allocate that memory block to the
array.
Array stores an extensive collection of similar data types. We have only three
variables, and then we can easily store them using separate names like int
var1, int var2, and int var3, but what if we have an extensive collection of
these variables? Then, we cannot assign separate names to each of them, as
it would be tedious and time-consuming.
Here comes the use of arrays in C, where we can store multiple data type
values in a contiguous memory block. Hence, we can use an array in C when
we are working with a large number of similar items.
You can easily access elements in an Array using their index. Suppose we
want to access the element at the third index of our array. Then, we can
easily do it using arr[2], which is size-1.
Also, you can quickly iterate through arrays with loops and conditional
statements. It allows quick and easy retrieval of data items.
Page |2
You need to initialise your array with some initial value at the time of
declaration. This is done because when declaring arrays in C, they contain
garbage values inside, which can alter our desired output. However, the
easiest way to initialise the array is using the method below.
arr [1] = 4;
arr [2] = 8;
arr [4] = 2;
Hence, we need to initialise our array with some starting value. There are a
few methods for declaring and initialising our array.
Let us initialise our array with a proper size and elements inside. This list
contains the elements enclosed within curly braces {}.
Array in C
Let us initialise our array without a proper predefined size and elements
inside. This list contains the elements enclosed within curly braces {}. In this
method, the compiler automatically detects the size of the array.
Page |3
Data_type name_of_array [ ] = { value1, value2,… … ,valueN};
In this declaration method, we will use the loops to declare our array. We will
use the loop to assign values to each element and insert them in our array.
Let us understand this with the help of the example we took above. Suppose
we have six elements: 1, 4, 8, 25, 2, 17. We want to insert them in a loop.
Array in C
#include <iostream>
int main() {
elements[i] = arr[i];
Page |4
cout << “ Array elements after initialization using for loop:” <<endl;
cout <<endl;
return 0;
Example of Array in C
Sorting in Array
#include<stdio.h>
void main ()
int i, j,temp;
Page |5
if(a[j] > a[i])
temp = a[i];
a[i] = a[j];
a[j] = temp;
Suppose we want to update any element at a given index inside our array,
then we simply need to use an assignment operator with the given syntax.
Name_of_array [ i ] = updated_value;
arr [ 0 ] = 9;
This will update the element at the index 0 with a new value i,e. 9.
Page |6
Types of C Array
1. One-Dimensional Array
name_of_Array [ size ];
2. Multi-Dimensional Array
This is also known as a multidimensional array because they have more than
one dimension. We can have N- D arrays in C. Let us check the syntax for the
N-D array.
Arrays are used to store a large set of similar data items in a contiguous
memory blocks. Let us check the advantages of the array in c.
Page |7
● As the elements are stored in a contiguous memory location, there are
no overhead costs involved. Hence, they are memory efficient.
Disadvantages of using Array in C
● Array in c has a fixed size, therefore we need to specify the size of the
array at the time of declaration.
● Using an array leads to memory wastage when you declare a size
more than the actual required.
● Insertion and deletion in an array are difficult. There are many other
efficient data elements for this purpose.
● Also, for searching any elements in an unsorted array we need to
traverse the whole array.
vv
Page |8