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 Array
  2. Two Dimensional Array
  3. Multi-Dimensional Array
    • Three Dimensional Array
    • Four Dimensional Array 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 Array_Name [Array_Size];
  • 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
  • Array_Name: This is the name you want to give it to an array. For example students, age, marks, employees, etc
  • Array_Size: Number of elements an array can hold or store. For example, Array_Size =10, then the array will hold 10 values.

For Example,

int Student_Age[5];

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

First Approach to Initialize Array in C

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 array 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 array 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 the array. 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 the array separately. Index value starts at 0 and ends at n-1, where n is the size of an array.

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 array_name[0] and to access or alter 5th value, then use array_name[4]. Let’s see the example of an Array in C for better understanding:

/* ARRAY IN C Example */

int Student_Age[5] = {5, 10, 15, 20, 25};

//To Access the values in the Student_Age[5] array

Printf("%d", Student_Age[0]); - It will display the First element of an array = 5

Printf("%d", Student_Age[3]); - It will display the Fourth element of an array = 20

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

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 array 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.

/* Array in C Example: Sum of numbers inside the Array */

#include  

int main()
{
 int i, Sum =0;
 int ExampleArray[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
 
 for (i=0; i<10; i++)
 {
   Sum = Sum + ExampleArray[i];

   printf("Addition of %d to the Sum = %d \n", ExampleArray[i], Sum);
 }
 
 printf("\nTotal Sum of the value in ExampleArray = %d \n", Sum);
 
 return 0;
}
Array in C

In this C array example, We declared an array called

ExampleArray[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

ExampleArray[10] is an integer array 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 + ExampleArray[i]; statement is used to add each and individual array element present in the ExampleArray[10]

Let us see the C array program execution in iteration wise

First Iteration : for (i = 0; i < 10;  i++)
Sum = Sum + ExampleArray[i]
Here, i value is 0, and sum = 0. So, Sum = Sum + ExampleArray[0]
Sum = 0 + 10 = 10

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

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

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

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

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

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

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

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

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

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

Things to Recall for Arrays

  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 the array can be accessed using index value.
  3. Index value starts at 0 and ends at n-1, where n is the size of an array.

Comments are closed.