Array in C

In this tutorial, we will learn about Array in C. Array is a collection of the same type(data type) of elements which stored at contiguous memory locations. We can create an array using ant primitive data type such as int, float, double, char, etc of any particular type. To store elements in an array and to fetch elements from an array we use index numbers. Index numbers start from 0. To understand this index system please check out the following illustration.

Declaring an Array in C:

It is really simple to declare an Array in C. You just have to mention data-type following with identifier and size of the array. To understand it check out the following Syntax to declare in C.

dataType arrayName[arraySize];

As in the above syntax you can see first of we defined data-type then identifier(array-name). After the identifier, we have to mention the size of the array(The number of elements an array can store).

Initializing an Array in C:

Initializing an array means providing elements to the array during declaration. Please check out the following example code to understand this.

Example Code:

int myArray[5] = {10, 5, 2, 9, 7};

//another way

//Here compiler will automatically get size of array according to number of elements
int myArray[] = {10, 5, 2, 9, 7}; 

Inserting values in an Array:

we can insert values in an array using an index number. we can mention on which index which value we want to insert. Please check out the following Example Code:

int myArray[5]; //declaring an array in c

myArray[0]=10;  //inseting value at 0 index
myArray[1]=20;  //inseting value at 1 index
myArray[2]=40;  //inseting value at 2 index
myArray[3]=35;  //inseting value at 3 index
myArray[4]=25;  //inseting value at 4 index

Fetching elements from an Array in C:

We know in array elements are stored at continues memory locations and we can access and write elements in an array using index number. To understand it we will create a example program. In which first of all we will create an array then we will fetch values from that array using index number.

Example Program:

#include <stdio.h>

int main()
{
    int myArray[]={10,5,2,15,17,3};
    printf("%d\n",myArray[0]);
    printf("%d\n",myArray[1]);
    printf("%d\n",myArray[2]);
    printf("%d\n",myArray[3]);
    printf("%d\n",myArray[4]);
    printf("%d\n",myArray[5]);
}

Output:

10                                                                                                                            
5                                                                                                                             
2                                                                                                                             
15                                                                                                                            
17                                                                                                                            
3 

Using loop to fetch array elements:

In the last example, you can see we are using the index number to fetch value at a particular index in an array. But suppose you have an array with 50 elements and you want to fetch all the elements from an array. In this kind of situation, you can use the looping statement to fetch elements from an array. Please check out the following example program to understand it.

#include <stdio.h>

int main()
{
    int myArray[]={10,5,2,15,17,3};
    
    // printing elements of an array
    for(int i = 0; i < 6; ++i) {
     printf("%d\n", myArray[i]);
    }
}

Output:

10                                                                                                                            
5                                                                                                                             
2                                                                                                                             
15                                                                                                                            
17                                                                                                                            
3 

Changing the value of an array element:

Same as fetching we can use the index number to update the value of an array element. Please check out the following example program.

Example Program:

#include <stdio.h>

int main()
{
    int myArray[]={10,5,2,15,17,3};
    
    myArray[1]=20; //changing value of element at index 1
    myArray[3]=12; //changing value of element at index 3
    
    printf("%d\n",myArray[0]);
    printf("%d\n",myArray[1]);
    printf("%d\n",myArray[2]);
    printf("%d\n",myArray[3]);
    printf("%d\n",myArray[4]);
    printf("%d",myArray[5]);
}

Output:

10                                                                                                                            
20                                                                                                                            
2                                                                                                                             
12                                                                                                                            
17                                                                                                                            
3  

 

Spread the love
Scroll to Top