Array in C

 Array in C

Array : It is largely used data structure in C programming language. It is a contiguous storage of elements of same type of data type. So size of each element of an array will be same. 

Total size of an array = (No. of elements) x (size of data-type)

Normally Index of an array is start from zero. Therefore, 

Index of 1st element  = 0
Index of last element = No. of elements in the array - 1

For example :-

int arr[7];            // arr is an array of 7 integer elements
First index of arr =  0
Last index of arr = 7-1 = 6


It is declared as shown below :

data-type array-name[size of array];

e.g. :-

int arr1[5]; // arr1 is an array of 5 integers
float arr2[10]; //arr2 is an array of 10 floating numbers
double arr3[10]; //arr3 is an array of 10 double type numbers


An array element can be accessed using the index of that element in the array.
Time Complexity of accessing an array element is O(1).
A value can be added or modified at a specific index in the array. 


An example program for array :-

#include<stdio.h>

int main()
{
int a;
printf("Enter the size of array\n");
scanf("%d", &a);

int arr[a];

for(int i=0; i < a; i++)
{
        arr[i] = 0;
}

    arr[0] = 6;
arr[3] = 5;

printf("Elements of the array are\n");
for(int i=0; i < a; i++)
{
    printf("%d\n", arr[i]);
    }

return 0;
}


// Output : 
Enter the size of array
5
Elements of the array are
6
0
0
5
0

Related Posts : 

Comments

Popular posts from this blog

Setting up a USB thermal printer with a Raspberry Pi 3B+

Autostart an app on reboot - Raspberry Pi

Basic Input/Output C Program