Arrays in C
Arrays in C
Arrays in C
What is Array in C?
C Array Declaration
In C, we have to declare the array like any other variable before using it. We
can declare an array by specifying its name, the type of its elements, and the
size of its dimensions. When we declare an array in C, the compiler allocates
the memory block of the specified size to the array name.
or
The C arrays are static in nature, i.e., they are allocated memory at the
compile time.
C
// C Program to illustrate the array declaration
#include <stdio.h>
int main()
{
return 0;
}
Output
C Array Initialization
In this method, we initialize the array along with its declaration. We use an
initializer list to initialize multiple elements of the array. An initializer list is the
list of values enclosed within braces { } separated b a comma.
If we initialize an array using an initializer list, we can skip declaring the size of
the array as the compiler can automatically deduce the size of the array in
these cases. The size of the array in these cases is equal to the number of
elements present in the initializer list as the compiler can automatically
deduce the size of the array.
array_name[i] = valuei;
C
// C Program to demonstrate array initialization
#include <stdio.h>
int main()
{
Output
Access Array Elements
array_name [index];
One thing to note is that the indexing in the array always starts with 0, i.e., the
first element is at index 0 and the last element is at N – 1 where N is the
number of elements in the array.
C
// C Program to illustrate element access using array
// subscript
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 15, 25, 35, 45, 55 };
return 0;
}
Output
Element at arr[2]: 35
Element at arr[4]: 55
Element at arr[0]: 15
We can update the value of an element at the given index i in a similar way to
accessing an element by using the array subscript operator [ ] and
assignment operator =.
array_name[i] = new_value;
C Array Traversal
Traversal is the process in which we visit every element of the data structure.
For C array traversal, we use loops to iterate through each element of the
array.
Array Traversal using for Loop
array_name[i];
C
// C Program to demonstrate the use of array
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
return 0;
}
Output
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They
are as follows:
The One-dimensional arrays, also known as 1-D arrays in C are those arrays
that have only one dimension.
Syntax of 1D Array in C
array_name [size];
Example of 1D Array in C
C
// C Program to illustrate the use of 1D array
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
return 0;
}
Output
Elements of Array: 1 0 1 4 9
C
// C Program to illustrate strings
#include <stdio.h>
int main()
{
// printing string
int i = 0;
while (arr[i]) {
printf("%c", arr[i++]);
}
return 0;
}
Output
Geeks
2. Multidimensional Array in C
Multi-dimensional Arrays in C are those arrays that have more than one
dimension. Some of the popular multidimensional arrays are 2D arrays and
3D arrays. We can declare arrays with more dimensions than 3d arrays but
they are avoided as they get very complex and occupy a large amount of
space.
A. Two-Dimensional Array in C
Syntax of 2D Array in C
array_name[size1] [size2];
Here,
C
// C Program to illustrate 2d array
#include <stdio.h>
int main()
{
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Output
2D Array:
10 20 30
40 50 60
B. Three-Dimensional Array in C
Syntax of 3D Array in C
C
// C Program to illustrate the 3d array
#include <stdio.h>
int main()
{
// 3D array declaration
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60 };
// printing elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n \n");
}
return 0;
}
Output
10 20
30 40
50 60
0 0
Arrays and Pointers are closely related to each other such that we can use
pointers to perform all the possible operations of the array. The array name is
a constant pointer to the first element of the array and the array decays to the
pointers when passed to the function.
C
// C Program to demonstrate the relation between arrays and
// pointers
#include <stdio.h>
int main()
{
Output
Address Stored in Array name: 0x7ffcab67d8e0
Address of 1st Array Element: 0x7ffcab67d8e0
Array elements using pointer: 10 20 30 40 50
To know more about the relationship between an array and a pointer, refer to
this article – Pointer to an Arrays | Array Pointer
// driver code
int main()
{
Output
Size of Array in main(): 20
Size of Array in Functions: 8
Array Elements: 10 20 30 40 50
C
// C Program to return array from a function
#include <stdio.h>
// function
int* func()
{
static int arr[5] = { 1, 2, 3, 4, 5 };
return arr;
}
// driver code
int main()
{
Output
Array Elements: 1 2 3 4 5
Note: You may have noticed that we declared static array using static
keyword. This is due to the fact that when a function returns a value, all the
local variables and other entities declared inside that function are deleted. So,
if we create a local array instead of static, we will get segmentation fault while
trying to access the array in the main function.
Properties of Arrays in C
It is very important to understand the properties of the C array so that we can
avoid bugs while using it. The following are the main properties of an array in
C:
1. Fixed Size
2. Homogeneous Collection
3. Indexing in Array
The array index always starts with 0 in C language. It means that the index of
the first element of the array will be 0 and the last element will be N – 1.
4. Dimensions of an Array
All the elements in the array are stored continuously one after another in the
memory. It is one of the defining properties of the array in C which is also the
reason why random access is possible in the array.
6. Random Access
The array in C provides random access to its element i.e we can get to a
random element at any index of the array in constant time complexity just by
using its index number.
C
// This C program compiles fine
// as index out of bound
// is not checked in C.
#include <stdio.h>
int main()
{
int arr[2];
return 0;
}
Output
0 0
In C, it is not a compiler error to initialize an array with more elements than the
specified size. For example, the below program compiles fine and shows just
a Warning.
C
#include <stdio.h>
int main()
{
return 0;
}
Output
Warnings:
Examples of Array in C
In this program, we will use scanf() and print() function to take input and print
output for the array.
C
// C Program to perform input and output on array
#include <stdio.h>
int main()
{
Output
Input
5 7 9 1 4
Output
Array Elements: 5 7 9 1 4
C
// C Program to the average to two numbers
#include <stdio.h>
int sum = 0;
// calculating cumulative sum of all the array elements
for (int i = 0; i < size; i++) {
sum += arr[i];
}
// returning average
return sum / size;
}
// driver code
int main()
{
Output
Array Elements: 10 20 30 40 50
Average: 30.00
C
// C Program to find the largest number in the array.
#include <stdio.h>
// Driver code
int main()
{
int arr[10]
= { 135, 165, 1, 16, 511, 65, 654, 654, 169, 4 };
return 0;
}
Output
Advantages of Array in C