Array in C refers to clubbing multiple similar entities together into a larger group in contiguous memory locations. These entities can belong to any data type, however in order to be stored together, they need to belong to the same data type. The ‘n’ elements are stored from left to right, with index numbers beginning from 0 and ending at n-1.
An array in C can be defined as the following syntax:
- Datatype means the type of data that has to be stored,
- Array name is the name of the array given by the user.
- Array size is the number of elements in an array.
Arrays are used to structurally organise and store a large number of variables. Array in C is a very important data structure and is widely used for various programming-related tasks. So if you need to store 500 variables, create an array of size 500, and store all the 500 variables in the array using a loop, thus enabling the storage of variables of a large amount in a single array in a more optimised manner.
Learn C# Basic
Last Updated: 2022-04-21
C# is one of the most and famous programming languages for developing web,windows applications and also video games.
Types of Arrays:
- Single Dimensional Array consists of elements of similar types which can be accessed via their indices. It is also known as 1-D.
- Multi-Dimensional Array, also known as 2-D, is a two-dimensional array in C, consists of elements which can array themselves, and as the name suggests, it has more than 2 dimensions as per required by the system’s compiler.
- Arrays store multiple values of the same data type in contiguous memory locations which enables efficient memory storage.
- Due to efficient storage capacity, the array enables quick access to data.
- You can have access to any random element in the array by calling out the index of the element.
- Array is simple and intuitive to use.
- Arrays support different data types like string, integer, etc.
- Arrays also represent a wide range of data structures, like lists, stacks or queues.
- Arrays can also be used to implement data algorithms and use for sorting and searching.
- Arrays are used to store a large number of variables.
Declaration and Initialisation of array in C:
There are multiple ways to declare and initialise data type in an array in C:
- Array Declaration by specifying the size:
You can declare the size by specifying the size or number of array elements, and this size represents the maximum number of elements held in an array. You can also declare an array by user-specified size. You need to allocate a value to declare an array, otherwise, it will be declared a garbage value.
- Array Declaration by initialising elements:
You can declare an array, by initialising elements where the compiler automatically allocates an array with a size equivalent to the number of elements in an array. So if you initialise at the time of declaration, though the array size isn’t specified, the compiler allocates size according to the elements in the array.
- Array initialisation using a loop:
The size of an array is declared first, and then an array is initialised using a for loop, and the loop then iterates from the first element to the end element (from index 0 to n-1th index in ‘n’ number of elements).