Array in C with Examples

This Tutorial explains an Array in C with Example(s).

What is an Array in C?

An array can be thought of as an arrangement of a specified number of elements of the same kind stored in contiguous memory locations and this arrangement is given a name.

Syntax of Array Declaration:

type name[size];

Here, Type is the Data type of an array (int or char).
Name – Name of the array.


int myArray[10];

Let’s try to declare ’12’ integers below,

    int jan = 1, feb = 2, march = 3, april = 4, may = 5, june = 6, july = 7,
      august = 8, september = 9, october = 10, november = 11, december = 12;
                                                  /* 12 integers declared */

Well! I’ve declared an initialized ’12’ integers. But what about if we need some 196 integers, let’s say, for population in 196 different countries. Is this declaration really easy? And what will happen when it comes to access such variables? When passing them in a function as arguments? Aren’t you in trouble? Fortunately, here is a way to fix this issue, declare an array, initialize it and use it!

Note: Join free Sanfoundry classes at Telegram or Youtube

Array Declaration and Initialization in C

    count[10];	      /* count an array of 10 something */

count is name of array immediately followed by a pair of subscripts ‘[]’ containing an expression which results in integer value specifying no. of elements in the array.

What’s type of 10 something? These something 10 can be integers, characters, floats etc. For example, if these are integers, then

    int count[10];     /* count an array of 10 integers */

if these something are characters, then

    char count[10];     /* count an array of 10 characters */
    char *count[10];    /* count an array of 10 strings */

and so on.

Since, an array contains more elements of same type, it’s generally called as Vector as opposite to a single variable called Scalar. Elements in an array are stored in locations with offsets ranging from 0 through size of array minus 1. For example,

    float marks[5] = {67, 89.5, 45.8, 60, 48.6};
                   /* marks contains 5 floats */

Let’s see how these 5 floats are organised in marks,

    marks[0] = 67;
    marks[1] = 89.5;
    marks[2] = 45.8;
    marks[3] = 60;
    marks[4] = 48.6;

Notice that offsets begin with ZERO and last ‘valid one’ is 1 less than size of array. These offsets are also called as Indices.

Different Types of Array

There are two kinds of arrays in C: Single Dimensional Arrays and Multi Dimensional Arrays.

Single Dimensional Array

Single-dimensional arrays are the simplest type of array and are used to store a list of items. It is also called one-dimensional array. To declare a one-dimensional array, the size of the array must be specified. For example, an array with 10 elements would be declared as int myArray[10];

Multi Dimensional Arrays

Multi-dimensional arrays are used to store data in a table format. They are often used to represent matrices or grids. To declare a multi-dimensional array, the number of dimensions and the size of each dimension must be specified. For example, a 2-dimensional array with 10 rows and 20 columns would be declared as int myArray[10][20];

Advantages of Array in C

  • They’re simple to use and understand.
  • Arrays are very efficient for storing and accessing data.
  • Arrays offer an easy way to keep track of related data items (i.e., by using indices).
  • It allows you to directly access any element in the array without having to loop through all of the elements. This can improve the efficiency of your code, especially if you need to access a large number of elements.

Disadvantages of Array in C

  • The size of the array must be fixed when it is created. This means that if you want to add or remove elements from the array, you must create a new array with the updated size.
  • Arrays are not always efficient. If you want to find an element in an array, you must go through each element until you find the one you’re looking for.
  • Inserting or deleting items from an array can be costly. This is because when you insert or delete an item from an array, all items in the array must be shifted in memory to make room for the new item or fill the gap left by the deleted item. This can take a lot of time, especially with large arrays.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.