An array in C programming language is a collection of similar types of elements (data type may be an integer, float, long, etc.). So, we can’t store multiple data type values in an array in this language. Instead of declaring a separate variable for each value, we can use one array to store multiple values.
In C Programming, we use [] square brackets after the array name to create one and use curly brackets {} to store the values. The one-dimensional array follows a linear data structure storing elements in contiguous memory locations. We must declare the size or array length at the time of declaration or initialization. Once the size is defined, the compiler allocates the memory to store the items. If it is a run-time initialization, the memory will fill with garbage values. Remember, once the size is defined, it is fixed and can’t be changed.
As mentioned earlier, an array accepts homogeneous or the same type (data type) values. For example, an integer array in C will store all the integer elements. If you try inserting a char value into that array, the compiler will throw an error. We can use the index position to access the array elements directly.
In our previous articles, we saw the variable declaration and initialization, which are pretty good for regular purposes. What if we want to store the salaries of 70 employees? Is it worth creating 70 variables and assigning those values? What happens if 100 employees? The C Programming language introduced the concept of an array to handle the issues.
Types of Arrays in C Programming
Generally, arrays are categorized based on their dimensions, and there are three types of arrays in this programming language:
These dimensions play a vital role in storing and managing data. For example, storing a linear collection, rows and columns, etc.
- One-Dimensional Array: It is the most basic and simplest form of an array in C Programming. It stores the same type of data in a linear sequence (single row). This article explains all the information about this single-dimensional array.
- Two-Dimensional Array: It is nothing but a matrix that stores data in rows and columns.
- A multi-dimensional array is also called an array of arrays (an extension to 2D arrays). If we add more dimensions to a one-dimensional array, it becomes multidimensional.
- Three-dimensional: A cubical representation by adding another dimension to the 2D array in C.
- Four Dimensional, etc.
- Jagged Array is an array of arrays where each inner array may hold different sizes of items.
- Character Array: In this Programming, the array of characters is called strings.
How to create an Array in C Programming?
There are two major steps involved in creating an array, and they are array declaration and initialization.
- Declaration: Specifying the array data type, name, and size.
- Initialization: Replacing the garbage values allocated by the compiler during declaration.
The above-mentioned two are the main pillars of creating an array in C language. However, there are other things we must learn to completely understand the array concept. It includes accessing the elements, finding the size, etc. The following sections provide a detailed explanation.
C Array Declaration Syntax
To declare an array, we must define the data type, name, and the size (or initialize the total number of elements). The syntax of the one-dimensional array in this Programming is as follows:
Data_Type ArrName [ArrSize];
If you observe the C syntax, it’s the same as any other variable declaration with additional square brackets [] after the array name.
- Data_type: It will decide the type (int, float, double, char, etc) of elements the array will accept. For example, if we want to store integers, then we declare the Data Type as int. To store floating-point values, we declare the Data Type as float, etc.
- Array_Name: This is the name you want to give to it. For example, students, age, marks, employees, etc
- Array_Size: Number of elements an array can hold or store. For example, if Arr_Size =10, it will store 10 values. Instead of defining the size, we can initialize the total elements, and the compiler automatically assigns the size.
For Example, int Student_Age[5];
- Here, we used int as the data type to declare an array in C Programming. So, it will only allow integers. If you try to add float values, then it will throw an error.
- Student_Age is the array name.
- The size of the Array is 5. It means Student_Age will only accept 5 integers.
- If we try to store more than 5, the compiler will throw an error.
- We can store less than 5. For Example, if we store 3 integer values, the remaining 2 will be assigned to the default value (0).
TIP: Some common use cases for using an array are storing student names and marks or employee names and salaries.
Initialization of an Array in C Programming
There are multiple ways to initialize an array, and it is important to understand these techniques to implement them within a program.
To initialize an array, all we have to do is place a set of comma-separated values within the curly braces {}. The first approach to declaring is
int Employees[5] = {1, 2, 3, 4, 5}
In this C Programming approach, we initialized the array only at the declaration time.
- Initializing the array at the declaration time is always the best practice. If you don’t know the values, initialize them to 0 or null.
- To initialize an entire array to zeros, use this code: int a[5] = {0}.
- Items in it can be accessed using an index value.
- The index value starts at 0 and ends at n-1, where n is the size.
Initialize without the size
Here, we haven’t mentioned the size. However, the compiler is intelligent enough to calculate the size of an array in C by checking the number of elements.
int Employees[ ] = {1, 2, 3, 4, 5}
Partial Initialization of an array in C
Here, we declared Employees with size 5, but we only assigned 3 variables. In this situation, the remaining values are assigned to default values (0).
int Employees[5] = {1, 4, 5}
The above-declared array example will be:
Employees[5] = {1, 4, 5, 0, 0}
It means
Employees[0] = 1
Employees[1] = 4
Employees[2] = 5
Employees[3] = 0
Employees[4] = 0
Partial with Specific elements initialization
As we mentioned earlier, when you assign fewer items (fewer than the original size) to an array, the C compiler fills them with zeros. However, we can use the index position while initializing an array.
In the example below, we declared an array of six elements. Among them, the first element is 10 and fills the remaining with zeros. However, [3] = 100 and [5] = 200 will initialize the elements at the index positions 3 and 5 with values 100 and 200.
#include <stdio.h>
int main()
{
int a[6] = {10, [3] = 100, [5] = 200};
for(int i = 0; i < 6; i++)
{
printf("%d\t", a[i]);
}
return 0;
}
OUTPUT
10 0 0 100 0 200
Array Initialization using a for loop in C Programming
The approaches mentioned above are good for storing a small number of elements in an array. What if we want to store 50, 100, or more values? Adding all of them using any of the above mentioned approaches will be a nightmare. To resolve this, we can use the loop concept and an array.
In this example, we have declared an integer array named Employees of 100 in size. Within the loop, we assign a value at each index position from 0 to 99. Here, the values are multiples of 2. They are: 0, 2, 4, 6, 8, 10,…,198.
int Employees[100];
for (int i =0; i < 100 ; i++)
{
Employees[i] = i*2;
}
Array Initialization without elements
In C programming, we can initialize an array without defining the elements. All we have to do is define the size (how many items an array can hold) and the data type (type of elements it accepts).
int a[6];
In the above line, we have declared an integer array that can hold 6 elements (int type). In the latter section of the program, we must assign the values explicitly. It can be done by using the index position. The following are the two options we use.
Manually initializing the items
This C example uses the index position of the three array elements and assigns some random values to that position.
#include <stdio.h>
int main()
{
int a[3];
a[0] = 99;
a[1] = 299;
a[2] = 499;
for(int i = 0; i < 3; i++)
{
printf("%d\t", a[i]);
}
return 0;
}
99 299 499
Run Time initialization using loops
Instead of manually assigning each item within the program, we can allow the user to enter their own values. The following C program initializes the array without elements. Next, the for loop allows the user to enter their own four integer values.
#include <stdio.h>
int main()
{
int a[4];
for (int i = 0; i < 4; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < 4; i++)
{
printf("%d\t", a[i]);
}
return 0;
}
OUTPUT
50 100 125 99
50 100 125 99
Access Elements of an Array in C Programming
We can use the index numbers to access the elements of an Array. Using the index position, we can access or alter/change each item present in it separately. By this, we can randomly access any element in an array. The index value starts at 0 and ends at n-1, where n is the size (the total number of items).
For example, if an array stores 5 elements, the index starts at 0 and ends with 4 (array size – 1). To access or alter the 1st value, use a[0], and to access or alter the 5th value, use a[4]. Let’s see the example of t for a better understanding:
#include <stdio.h>
int main()
{
int Student_Age[5] = {5, 10, 15, 20, 25};
//To Access the values in the Student_Age[5]
printf("%d", Student_Age[0]); // It will display the First element = 5
printf("\n%d", Student_Age[3]); // It will display the Fourth element = 20
//To Alter the values in the Student_Age
Student_Age[2] = 56; // It will change the value of Student_Age[2] from 15 to 56
Student_Age[4] = 92; // It will change the value of Student_Age[4] from 25 to 92
printf("\n%d", Student_Age[2]);
printf("\n%d", Student_Age[4]);
return 0;
}
The final output will be {5, 10, 56, 20, 92}
5
20
56
92
Update Array Element
After the array initialization, the C programming language provides an opportunity to change existing items. For this, we must use the index number to point to the value that we want to change. Next, use the assignment operator to change/update the existing value.
To demonstrate the same, we have declared an integer array with five numeric values. If you observe the code below, we have changed the index positions 0 and 2 of the num array values. Here, we updated 9 with 500 and 27 with 100.
NOTE: Changing the value means permanently updating the existing value with the new.
#include <stdio.h>
int main()
{
int num[] = {9, 18, 27, 36, 45};
// Updating 27 (index position 2) with 100
num[2] = 100;
//Changing 9 (index position 0) with 500
num[0] = 500;
printf("%d", num[2]);
printf("\n%d", num[0]);
return 0;
}
OUTPUT
100
500
To display the complete array, use the following for loop code. Please place it before the return statement.
for(int i = 0; i <= 4; i++)
{
printf("%d\t", num[i]);
}
Input and Output Array Items
Like any other variable, we can use the C Programming language scanf() and printf() statements to input and output the array items. For example, the following scanf() statement reads the user-input and stores the integer value as the third array element (second index position).
scanf("%d", a[2]);
The below printf() statement returns the output to the terminal. It returns the array element at the 3rd index position (4th element).
printf("%d", a[3]);
How to find the size of an array?
In C programming, the compiler allocates a continuous block of memory to store the one-dimensional array elements. We can use the sizeof operator to find the size of an array. However, instead of the total number of elements, the sizeof operator returns the total number of bytes.
The size or memory allocation depends upon the data type of an array. For instance, for an integer array, the size of each item in terms of bytes is 4. Let’s say we have initialized the array of 10 elements with the following data types. In all five arrays, the total number of elements or the actual size is the same, i.e., 10.
- Char = 1 byte -> Total Bytes = 10.
- Short = 2 bytes -> Total bytes = 20.
- Int = 4 bytes -> Total bytes = 40.
- Float = 4 bytes -> Total bytes = 40.
- Double = 8 bytes -> Total bytes = 80.
#include <stdio.h>
int main()
{
char a[5];
printf("Char = %ld", sizeof(a));
printf("\t%ld", sizeof(a) / sizeof(a[0]));
short b[5];
printf("\nShort = %ld", sizeof(b));
printf("\t%ld", sizeof(b) / sizeof(b[0]));
int c[5];
printf("\nInteger = %ld", sizeof(c));
printf("\t%ld", sizeof(c) / sizeof(c[0]));
float d[5];
printf("\nFloat = %ld", sizeof(d));
printf("\t%ld", sizeof(d) / sizeof(d[0]));
return 0;
}
OUTPUT
Char = 5 5
Short = 10 5
Integer = 20 5
Float = 20 5
Here, the sizeof(b) returns the total number of bytes in a short array. To get the actual size (the total number of elements), we must divide it by the byte size of one element. Here, the byte size of a short is two. So, 10 (total bytes) / 2 (short size) = 5 (actual elements).
How to Traverse an Array in C Programming?
Array traversal or iterating is the process of sequentially accessing each element in an array. Generally, we use the index positions to access individual array items. However, to access the whole array, there is a lot of repetition in the code.
In C Programming, we can use loops to perform array iteration or traversal. The for loop is the most popular approach for array traversal, but you can also use the while and do while loop.
With the help of a loop, we can access each item in an array and perform operations on it. Writing a single line of code inside a loop will operate on all items. We must follow the process below to perform the array traversal.
- Initialize the loop variable to the starting index position of an array in C. It means the array traversal starts from the beginning.
- The condition ensures that the iteration must end at the last index position (n – 1).
- Increment the loop counter variable by one to go to the next array item.
- Within the loop, use the counter variable as the index position to access or perform operations on each element in an array.
In the example below, we declare an integer array of five elements. Next, we use the for loop to iterate over the array and print the items in normal and reverse order.
#include <stdio.h>
int main()
{
int a[5] = {10, 20, 30, 40, 50};
printf("List of Items from start to end: ");
for(int i = 0; i < 5; i++)
{
printf("%d\t", a[i]);
}
printf("\nPrinting Array Items in Reverse Order: ");
for(int i = 4; i >= 0; i--)
{
printf("%d\t", a[i]);
}
return 0;
}
List of Items from start to end: 10 20 30 40 50
Printing Array Items in Reverse Order: 50 40 30 20 10
Character Array in C Programming
A string is a character array (sequence of characters). We have already explained the string in a separate article, so please use the hyperlink.
We can use the %s format specifier to print the whole message or use the index position to access individual characters.
#include <stdio.h>
int main()
{
char msg[] = "Hello";
printf("%s", msg);
printf("\n%c", msg[1]);
return 0;
}
Hello
e
How to find the Memory Address of Array Items?
In C programming, we can use the pointer (&) to find the memory address of each item in an array. In the following example, we declared an integer array of six elements. The for loop iterates over them, and we used the index position from 0 to 5 to print the elements. Here, &a[i] returns the address of each item at that particular index position.
#include <stdio.h>
int main()
{
int a[6] = {9, 18, 27, 36, 45, 54};
for(int i = 0; i < 6; i++)
{
printf("a[%d]: %d \tAddress: %d\n", i, a[i], &a[i]);
}
return 0;
}
OUTPUT
a[0]: 9 Address: 6291040
a[1]: 18 Address: 6291044
a[2]: 27 Address: 6291048
a[3]: 36 Address: 6291052
a[4]: 45 Address: 6291056
a[5]: 54 Address: 6291060
If you observe the above output, you will notice that the memory addresses are continuous, with a standard 4-byte difference between each item. Remember, for an integer array, each item occupies 4 bytes.
Avoid Mixed Data types
In C programming, we must declare an array that stores items of the same data type, and we must not mix different values. If the compiler finds different data types in a single array, in some cases (auto type conversion), it performs auto conversion. In some cases, it throws an error.
In this example, we have declared an array of integers and floating-point numbers. However, if you observe the output, the decimal values are auto-truncated and converted to integers.
#include <stdio.h>
int main()
{
int a[5] = {9, 18.20, 27, 36.11, 45};
for(int i = 0; i < 5; i++)
{
printf("%d\t", a[i]);
}
return 0;
}
9 18 27 36 45
How to print items of an array in C?
As mentioned in the access items and the array traversal section, we can use the index position to print individual items. On the other hand, use a for loop, a while loop, or do while loop to print the complete array.
If we declare an array with a pre-defined size, we can use the same number to control the for loop counter variable. So that it does not exceed the total number of elements. However, if we initialize the values without defining the size (done by the compiler), we must use the sizeof operator to find the length.
#include <stdio.h>
int main()
{
int a[] = {5, 10, 15, 20, 25};
int size = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < size; i++)
{
printf("%d\t", a[i]);
}
return 0;
}
5 10 15 20 25
Array in C Programming Example
In this program, we will declare an integer array with a size of 10. We will sum those 10 values and display the output.
#include <stdio.h>
int main()
{
int i, Sum = 0;
int ExArr[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
for (i = 0; i < 10; i++)
{
Sum = Sum + ExArr[i];
printf("Addition of %d to it = %d \n", ExArr[i], Sum);
}
printf("\nTotal Sum of the value in ExArr = %d \n", Sum);
return 0;
}
TIP: Within the program article, apart from the above, many examples demonstrate all kinds of operations on arrays.

In this C array example, we declared the following one.
ExArr[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
ExArr[10] is an integer with size 10 (index values are 0 to 9). We assigned those 10 values at the declaration time.
for (i=0; i<10; i++) statement, start the iteration at 0 and perform it until it reaches nine because our index value ends at 9.
Sum= sum+ExArr[i]; statement is used to add each and individual element present in the ExArr[10]
Let us see the C array program execution in iteration wise.
First Iteration : for (i = 0; i < 10; i++)
Sum = Sum + ExArr[i]
Here, the i value is 0, and the sum = 0. So, Sum = Sum + ExArr[0]
Sum = 0 + 10 = 10
Second Iteration: for (i = 1; 1 < 10; 1++)
Sum = Sum + ExArr[1] => 10 + 20 = 30
3rd Iteration: for (i = 2; 2 < 10; 2++)
Sum= Sum + ExArr[2] => 30 + 30 = 60
4th Iteration: for (i = 3; 3 < 10; 3++)
Sum= 60 + ExArr[3] => 60 + 40 = 100
Fifth Iteration: for (i = 4; 4 < 10; 4++)
Sum= 100+ ExArr[4] => 100 + 50 = 150
Sixth Iteration: for (i = 5; 5 < 10; 5++)
Sum= 150+ ExArr[5] => 150 + 60 = 210
7th Iteration: for (i = 6; 6 < 10; 6++)
Sum= 210 + ExArr[6] => 210 + 70 = 280
8th Iteration: for (i = 7; 7 < 10; 7++)
Sum = 280 + 80 (ExArr[7]) = 360
9th Iteration: for (i = 8; 8 < 10; 8++)
Sum = 360 + 90 (ExArr[8]) = 450
10th Iteration: for (i = 9; 9 < 10; 9++)
Sum= 450 + 100 (ExArr[9]) = 550
The condition inside the for loop (10 < 10) will fail for the next iteration.
To calculate the average of an array in C Programming language, use the code below. Please write these two lines before the return statement.
double average = (double)Sum / 10;
printf("Average = %.2lf", average);
Comments are closed.