C Arrays

by anupmaurya
5 minutes read

C Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.

An array is a variable that can store multiple values. For example, if you want to store 50 integers, you can create an array for it.

int data[50];

How to declare an array?


dataType arrayName[arraySize];


float mark[10];

Here, we declared an array, mark, of floating-point type. And its size is 10. Meaning, it can hold 10 floating-point values.

  • The size and type of an array cannot be changed once it is declared.
  • Always, Contiguous (adjacent) memory locations are used to store array elements in memory.

How to access element of an array in C

You can use array subscript (or index) to access any element stored in an array. Subscript starts with 0, which means arr[0] represents the first element in the array arr.

In general arr[n-1] can be used to access nth element of an array. where n is any integer number.

For example:

int mydata[20];
mydata[0] /*     first element of array mydata*/
mydata[19] /*  last (20th) element of array mydata*/

How to initialize an array?

It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

Here, we haven’t specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.Un-initialized array always contains garbage values.

Input data into the array

Here we are iterating the array from 0 to 3 because the size of the array is 4. Inside the loop we are displaying a message to the user to enter the values. All the input values are stored in the corresponding array elements using scanf function.

for (x=0; x<4;x++)
    printf("Enter number %d \n", (x+1));
    scanf("%d", &num[x]);

Reading out data from an array

Suppose, if we want to display the elements of the array then we can use the for loop in C like this.

for (x=0; x<4;x++)
    printf("num[%d]\n", num[x]);

Change Value of Array elements

int mark[5] = {19, 10, 8, 17, 9}

// make the value of the third element to 1
mark[2] = 1;

// make the value of the fifth element to 45
mark[4] =45;


Calculate Average

// Program to find the average of n numbers using arrays

#include <stdio.h>
int main()
     int marks[10], i, n, sum = 0, average;

     printf("Enter number of elements: ");
     scanf("%d", &n);

     for(i=0; i<n; ++i)
          printf("Enter number%d: ",i+1);
          scanf("%d", &marks[i]);
          // adding integers entered by the user to the sum variable
          sum += marks[i];

     average = sum/n;
     printf("Average = %d", average);

     return 0;


Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39


There are 2 types of C arrays. They are,

  1. One-dimensional array in C: One-dimensional arrays are declared by providing one set of square [ ] brackets after the variable name in the declaration statement. What you have learned above is a one-dimensional array.
  1. Multi-dimensional array in C: Multi-dimensional arrays are declared by providing more than one set of square [ ] brackets after the variable name in the declaration statement.
    • Two-dimensional array
    • Three-dimensional array
    • Four-dimensional array etc.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.