- C Programming
- C Tutorial
- C Basic Syntax
- C Data Types
- C if...else Statement
- C switch Statement
- C for Loop
- C while Loop
- C do...while Loop
- C Jump Statements
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Library Functions
- C Recursion
- C Variable Scope
- C Structures
- C Linked List
- C Stacks
- C Queues
- C Binary Tree
- C Header Files
- C File I/O
- C Programming Examples
- C Programming Examples
Arrays in C Programming with Examples
An array is simply a collection of variables of the same type that share a common reference. Or you can think of an array as a group of variables with the same type that share a name. In order to get to a certain item in an array, you need to use its index. The lowest address corresponds to the first element, while the highest address corresponds to the final element.
Note: In an array, the index starts at 0 always.
Declare an array in C
Here is the general form to declare an array in C:
type arr_name[size];
Here "type" is any valid data type, "arr_name" is the name of the array you give, and "size" is the array size. In other words, size specifies the number of elements that an array can hold.
Initialize an array in C
Here's an example of how to initialize an array in C. Here, we create a five-element array:
int arr[5] = {1, 2, 3, 4, 5};
Here, arr is the name of the array of type int containing five elements, i.e., 1, 2, 3, 4, and 5.
What if you omit the array size?
If the array size is omitted, the array becomes large enough to hold the initialization. Let's look at the following code fragment:
int arr[] = {1, 2, 3, 4, 5};
Here you will create exactly the same array as you did above, but you can also initialize more than or fewer than 5 elements in this array. Like
int arr[] = {1, 2, 3};
or
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Access Array Elements in C
An array element is accessed by indexing the array name. This can be done by placing the index number of that element within the square bracket after the name of the array. Let's look at this code fragment. Here we are going to access the value present at index number 3 from the array named arr.
int val = arr[3];
Now the variable val has the same value as the value present at the index 3 of the array arr.
Types of Array
Arrays can have from one to several dimensions. Following are the two types of arrays:
Now let's discuss the one-dimensional array in detail.
One-dimensional Arrays in C
As previously discussed, you can declare a one-dimensional array, initialize a one-dimensional array, and access one-dimensional array elements in the same way. Let's look at an example.
#include <stdio.h> int main() { int arr[] = {12, 43, 54, 7, 120, 430}; int i, tot; printf("The first element = %d", arr[0]); printf("\nThe second element = %d", arr[1]); tot = sizeof(arr)/sizeof(arr[0]); printf("\nThe last element = %d", arr[tot-1]); return 0; }
The output of the above program is
The first element = 12 The second element = 43 The last element = 430
In the above program, the code "sizeof(arr)" returns the size of "arr" in bytes, and the code "sizeof(arr[0])" returns the size of the first element (which is 12; an integer-type value) of the array "arr." Therefore, the following statement
tot = sizeof(arr)/sizeof(arr[0]);
initialize the number of elements available in the array named "arr." The above statement can also be written as:
tot = sizeof(arr)/sizeof(int);
Because the array's first element is "12," which is an integer value. As a result, we will eventually get the size of "int" in our platform.
Let me create another example of an array in C.
#include <stdio.h> int main() { int arr[10], i; printf("Enter 10 array elements: "); for (i = 0; i < 10; i++) scanf("%d", &arr[i]); printf("\nThe array elements and their respective indexes are as follows: \n"); for (i = 0; i < 10; i++) printf("arr[%d] = %d\n", i, arr[i]); return 0; }
The following snapshot shows the initial output produced by the above program:
Now type the first element, say 23, and hit the ENTER key, then type the second number, say 43, and hit the ENTER key again, and so on. In this case, 10 numbers correspond to 10 array elements.The following snapshot shows the sample run:
Multi-dimensional Arrays in C
Multidimensional arrays are also supported by the C programming language. A two-dimensional array is the most basic type of multi-dimensional array. In C, the following is the general syntax for declaring a two-dimensional array:
int arr[10][5];
A two-dimensional array is equivalent to an array of one-dimensional arrays. As a result of the preceding declaration, int arr[10][5] can be considered a ten one-dimensional array where each array is of size 5 or contains 5 elements. The first index can be thought of as a row, and the second index as a column. As a result, the above array has ten rows and five columns. Let's look at an example.
#include <stdio.h> int main() { int arr[5][3], i, j; printf("Enter 15 elements for a 5x3 matrix:"); for (i = 0; i < 5; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } printf("\nThe Matrix is:\n"); for (i = 0; i < 5; i++) { for (j = 0; j < 3; j++) { printf("%d\t", arr[i][j]); } printf("\n"); } return 0; }
The output with user inputs 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15 as fifteen elements for the 5x3 matrix should be:
Enter 15 elements for a 5x3 matrix:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The Matrix is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The C programming language supports arrays with more than two dimensions. A multi-dimensional array declaration takes the following general form:
type array_name[size1][size2][size3]...[sizeN];
Consider the following example illustrating the multi-dimensional array in C:
#include <stdio.h> int main() { int my_array[1][2][3][4], i, j, k, l, val = 1; // initializing array for (i = 0; i < 1; i++) { for (j = 0; j < 2; j++) { for (k = 0; k < 3; k++) { for (l = 0; l < 4; l++) { my_array[i][j][k][l] = val; val++; } } } } // displaying array for (i = 0; i < 1; i++) { for (j = 0; j < 2; j++) { for (k = 0; k < 3; k++) { for (l = 0; l < 4; l++) { printf("my_array[%d][%d][%d][%d] = %d\n", i, j, k, l, my_array[i][j][k][l]); } } } } return 0; }
The output should be:
my_array[0][0][0][0] = 1 my_array[0][0][0][1] = 2 my_array[0][0][0][2] = 3 my_array[0][0][0][3] = 4 my_array[0][0][1][0] = 5 my_array[0][0][1][1] = 6 my_array[0][0][1][2] = 7 my_array[0][0][1][3] = 8 my_array[0][0][2][0] = 9 my_array[0][0][2][1] = 10 my_array[0][0][2][2] = 11 my_array[0][0][2][3] = 12 my_array[0][1][0][0] = 13 my_array[0][1][0][1] = 14 my_array[0][1][0][2] = 15 my_array[0][1][0][3] = 16 my_array[0][1][1][0] = 17 my_array[0][1][1][1] = 18 my_array[0][1][1][2] = 19 my_array[0][1][1][3] = 20 my_array[0][1][2][0] = 21 my_array[0][1][2][1] = 22 my_array[0][1][2][2] = 23 my_array[0][1][2][3] = 24
C Arrays Examples
The following is a list of some of the most well-known example programs in C that use an array.
- Search an element in an array using linear search in C
- Search an element in an array using binary search in C
- Find the largest element in an array in C
- Find the smallest element in an array in C
- Reverse an array in C
- Insert an element into an array in C
- Delete an element from the array in C
- Merge two arrays in C
- Sort an array using the bubble sort in C
- In C, use the selection sort to sort an array
- Sort an array using the insertion sort in C
- Add two matrices in C
- Subtract two matrices in C
- Multiply two matrices in C
« Previous Tutorial Next Tutorial »