C Array

In this tutorial you will learn about the C Array and its application with practical example.

In C programming, an array is ordered collection of homogeneous elements placed in contiguous memory locations. In c, an array is a derived data type that can be used to store collection of any primitive data types such as int, char, double, float, double, etc. In order to store multiple values together in array, all the elements should be of the same data type. Array elements can be accessed individually by adding an unique index or subscript to it. Arrays are typed, thus once you declare the type of the array then you would only have elements of the same type.

An array variable is useful in c when we want to hold multiple values of single data type in single variable. Suppose, you want to store marks of 10 students then you need to declare 10 different variables to hold marks like student1_marks, student2_marks etc. But with the help of an array you can store marks of all students in single variable. Given below is a graphical representation of an array.

graphical-representation-of-array

 

Array is used to store multiple values in a single variable, instead of declaring separate variables for each value. An array is made up of a key and a value pair, and the key points to the value. Arrays may be of any variable type. In this tutorial you will learn how to declare, initialize and access array elements with the help of examples.

Types of Array In C

In this tutorial, we will learn about the various types of array in c programming. All array types provide the same basic functionality. In C programming we have the following two array types:

  • One Dimensional Array
  • Multi-dimensional array

One Dimensional Array

One dimensional array is the simplest form of arrays found in C. A one-dimensional array is a structured collection of array elements that can be accessed individually by specifying the position of an element with a single index value. Learn how to declare and initialize one dimensional array. We will also learn to access array elements in one dimensional array.

Syntax:-

General syntax to declare a one dimensional array is as follows:

Example:-

Given below is a simple example to declare a one dimensional array in c programming:

Multi-dimensional array

In c language, we are allowed to create multi-dimensional array. Multi-dimensional arrays is nothing just an array of arrays. Learn how to declare and initialize multi-dimensional array. We will also learn to access array elements in multi-dimensional array.

Syntax:-

General syntax to declare a multi-dimensional array is as follows:

Example:-

Given below is a simple example to declare a multi-dimensional array in c programming:

Declaring Array In C

There are multiple ways we can declare an array in c. An array can be declared by specifying array type, array name followed by size of the array inside square brackets []. Optionally you can initialize array in c by passing a list of initial values inside square brackets [].

Declaring Array by Specifying the Size

An array can be declared by specifying the size or the number of elements. The size of the array specifies the maximum number of elements that the array can hold. General syntax to declare an array by specifying size is as follows:

Syntax:-

An array can be declared by specifying data type, name of the array followed by size of the array inside square brackets []. The arraySize specifies the maximum number of elements that the array can hold. The type must be any valid C data type such as double, float, short etc.

Example:-

Here, we have declared an integer array marks with array size 5. The array size 5 means it can store 5 integer values

Declaring Array by Initializing Elements

An array can be initialized at the time of its declaration. Declaring array by initializing elements allows compiler to allocate array of size equal to the number of elements in array. General syntax to declare an array by initializing elements is as follows:

Syntax:-

Example:-

In the above example we have declared an array with 5 elements. Even though we have not specified the array size, the compiler will allocate memory size of 5 integer elements for it.

Declaring Array by Specifying the Size and Initializing Elements

An array can also be declared by specifying the array size and initializing array elements at the same time. In this method of array declaration, if the number of elements is less than the array size specified, then the compiler will automatically initialize rest of the elements with 0 value.

Syntax:-

Example:-

In the above example we have declared an array with array size 5, but we have initialized it with only 4 elements. Even though we have not initialized 5 elements, the compiler will initializes first 4 elements as specified by user and rest elements as 0. The above array declaration is same as given below:

Initializing Array In C

Initializing Array In C is process to declare and provide with initial element values. An array can be initialized in multiple ways same array declaration. The simplest way to initialize an array is to initialize individual array elements one by one using the index. Consider the following example.

In the above example, we have first declared the array and then later we have initialized it with individual element values. However you can also initialize the array during declaration in a single statement as follows:

Here, the number of elements inside braces { } should not exceed the array size we specified between square brackets [ ]. You can also initialize an array like this.

Here, we have not specified the array size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.

Note:- Un-initialized array always contain garbage values.

Accessing Array Elements

Accessing Array Elements in c is to refer individual array element value. It is quick and easy to access array element. Array elements can be accessed using an integer index. Array index starts from 0 and goes till size of array minus 1. The last element of the array can be accessed as arrayName[size-1]. Here, the size is equal to the number of elements in array. Suppose, you declare an array like below:

If you want to access the first element of the array, then you can access it by arr[0]. And if you want to access the second element of the array then you can do it using arr[1] and so on. Some of the important facts about array indexing is as follows:

  • The first index of the array is 0 not 1.
  • To access the last element of the array, do arrayName[size-1].

Properties of array in C

Since, array is an important concept in C language. It has many array properties associated with it. Below are some of the properties of array in C:

  • An array can store a fixed number of elements.
  • Elements of the array stored at contiguous memory locations.
  • Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.
  • An array is a variable that can only store elements of the same data type.
  • You can access any of the array element randomly.

Advantages of array in C

Array is an important concept in C language. The arrays provides many advantages to the programmers while programming. Some of important advantages of array in c are as follows:

Code Optimization:- It helps us to write more optimized and clean code, since we can store multiple values in a single array variable at once.

Ease of Access:- It is easy to access individual or all array elements from array. It is also possible to access array elements random or in any order.

Ease of sorting:- Array makes it easy to sort elements with just a few lines of code.

Ease of traversing :- Traversing through array elements becomes easy using a loop statement.

Disadvantages of array in C

We have discussed several advantages of array in c, but it also have some disadvantages. Below are some disadvantages of array in C:

Fixed Size:- Array size has to be fixed size. This means that once array size is initialized, it can not be increased or decreased.

Homogeneous :- Since arrays are homogeneous, we can only store a single type of element in an array.

Example:-

Output:

 

In this tutorial we have learn about the C Array and its application with practical example. I hope you will like this tutorial.