What is an Array in C?

Array in C is a collection of similar types of elements (Type may be an integer, float, and long, etc.). So, in C programming, we can’t store multiple data type values in an array.

For example, an integer array in C will store all the integer elements. If you try to insert a float or char value into that array, then the C compiler will throw an error.

In our previous articles, we saw the variable declaration, initialization, and they are pretty good for regular purposes. What if we want to store 50 employees’ salaries?. Is it worth to create 50 variables and assign those values? What happens if it is 100 employees?. C Programming language introduced the concept of the array in C to handle the issues.

Types of Array in C

There are three types of arrays in C Programming:

  1. One Dimensional
  2. Two Dimensional
  3. Multi-Dimensional
    • Three Dimensional
    • Four Dimensional etc

NOTE: In C Programming, the array of characters called as strings

Syntax of an Array in C

The syntax of One Dimensional Array in C Programming is as follows:

Data_Type ArrName [ArrSize];
  • Data_type: It will decide the type of elements array will accept. For example, If we want to store integer values then we declare the Data Type as int, If we want to store Float values then we declare the Data Type as float, etc
  • ArrName: This is the name you want to give it to an array. For example students, age, marks, employees, etc
  • Arr_Size: Number of elements an array can hold or store. For example, Arr_Size =10, then it will hold 10 values.

For Example,

int Student_Age[5];

  1. Here, we used int as the data type to declare an array. So, it will accept only integers. If you try to add float values, then it will throw an error.
  2. Student_Age is the name
  3. The size of an Array is 5. It means Student_Age will only accept 5 integer values.
    • 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 values will be assigned to the default value (Which is 0).

Array in C Initialization

There are multiple ways to initialize an array and the first approach to declaring is

int Employees[5] = {1, 2, 3, 4, 5}

Here, We initialized the array at the declaration time only.

Second Approach to Initialize Array

int Employees[ ] = {1, 2, 3, 4, 5}

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.

Third Approach to Initialize Array in C

int Employees[5] = {1, 4, 5}

Here we declared Employees with size 5, but we only assigned 3 variables. In this situation, the remaining values assigned to default values (0 in this case).

The above C array 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

Fourth Approach of Array in C Initialization

The above 3 approaches are good to store a small number of elements into it. What if we want to store 50, 100, or more values. It will be a nightmare to add all of them using any of the approaches mentioned above. To resolve this, we can use the loop concept along with array in C:

int i, Employees[100];

for (i =0; i < 100 ; i++)
 {
   Employees[i] = i*2;
 }

Access Elements of an Array in C programming

We can access the elements of an Array in C programming using indexes. Using the index, we can access or alter/change each item present in it separately. Index value starts at 0 and ends at n-1, where n is the size.

For example, if an array in C stores 5 elements, the index starts at 0 and ends with 4. To access or alter 1st value, use ArrName[0] and to access or alter 5th value, then use ArrName[4]. Let’s see the example of an Array in C for better understanding:

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("%d", Student_Age[3]); - It will display the Fourth element = 20

//To Alter the values in the Student_Age[5]

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

The final output will be {5, 10, 56, 20, 92}

Array in C Programming Example

In this program, We will declare an integer array in C with size 10, and then we will sum those 10 values and displays 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;
}
C Array

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 the iteration 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, i value is 0, and sum= 0. So, Sum = Sum + ExArr[0]
Sum = 0 + 10 = 10

Second Iteration: for (i = 1; 1 < 10; 1++)
Sum = Sum + ExArr[1]
Sum = 10 + 20 = 30

Third Iteration: for (i = 2; 2 < 10; 2++)
Sum= Sum + ExArr[2]
Sum = 30 + 30 = 60

Fourth Iteration: for (i = 3; 3 < 10;  3++)
Sum= Sum + ExArr[3]
Sum = 60 + 40 = 100

Fifth Iteration: for (i = 4; 4 < 10;  4++)
Sum= Sum+ ExArr[4] = 100 + 50 = 150

Sixth Iteration: for (i = 5; 5 < 10;  5++)
Sum= Sum+ ExArr[5] = 150 + 60 = 210

Seventh Iteration:  for (i = 6; 6 < 10;  6++)
Sum= Sum+ ExArr[6]
Sum = 210 + 70 = 280

Eighth Iteration:  for (i = 7; 7 < 10;  7++)
Sum= Sum + ExArr[7]
Sum = 280 + 80 = 360

9th Iteration: for (i = 8; 8 < 10; 8++)
Sum = Sum + ExArr[8] = 360 + 90 = 450

10th Iteration:  for (i = 9; 9 < 10;  9++)
Sum= Sum + ExArr[9] = 450 + 100 = 550

For the next iteration, the condition inside the for loop (10 < 10) will fail.

Things to Recall

  1. It is always best practice to initialize the array in C at the declaration time. If you don’t know the values, then initialize to 0 or null.
  2. Items in it can be accessed using index value.
  3. Index value starts at 0 and ends at n-1, where n is the size.

Comments are closed.