Array in C programming is a collection of similar types of elements (Type may be an integer, float, long, etc.). So, we can’t store multiple data type values in an array in this C programming language.
For example, an integer array in C will store all the integer elements. If you try inserting a float or char value into that array, the compiler will throw an error.
In our previous articles, we saw the variable declaration and initialization, which are pretty good for regular purposes. What if we want to store 50 employees’ salaries? Is it worth creating 50 variables and assigning those values? What happens if it is 100 employees? C Programming language introduced the concept of the array to handle the issues.
Types of Array in C Programming
There are three types of arrays in C Programming:
- One Dimensional
- Two Dimensional
- Multi-Dimensional
- Three Dimensional
- Four Dimensional etc
NOTE: In this Programming, the array of characters called strings
C Array Syntax
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 the array will accept. For example, If we want to store integers, then we declare the Data Type as int. If we want to store floating-point values, then we declare the Data Type as float, etc
- ArrName: This is the name you want to give to it. For example, students, age, marks, employees, etc
- Arr_Size: Number of elements an array can hold or store. For example, if Arr_Size =10, it will hold 10 values.
For Example,
int Student_Age[5];
- Here, we used int as the data type to declare an array in c programming. So, it will accept only integers. If you try to add float values, then it will throw an error.
- Student_Age is the name.
- The size of an 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).
Initialization of Array in C programming
There are multiple ways to initialize an array in C, 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
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
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 are assigned to default values (0).
The above-declared C 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
Fourth Approach of Initialization
The above 3 approaches are good for storing a small number of elements into it. What if we want to store 50, 100, or more values? Adding all of them using any of the abovementioned approaches will be a nightmare. To resolve this, we can use the C loop concept and the array.
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 using indexes in C. Using the index; we can access or alter/change each item present in it separately. The index value starts at 0 and ends at n-1, where n is the size.
For example, if an array stores 5 elements, the index starts at 0 and ends with 4. To access or alter the 1st value, use ArrName[0], and to access or alter the 5th value, then use ArrName[4]. Let’s see the example of t for a 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 C program, We will declare an integer array with a size of 10. 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; }
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, 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]
Sum = 10 + 20 = 30
Third Iteration: for (i = 2; 2 < 10; 2++)
Sum= Sum + ExArr[2]
Sum = 30 + 30 = 60
Fourth Iteration of C array: 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 of the array in c programming: for (i = 7; 7 < 10; 7++)
Sum = 280 + 80 (ExArr[7]) = 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
The condition inside the for loop (10 < 10) will fail for the next iteration.
Things to Recall
- Initializing the C array at the declaration time is always the best practice. If you don’t know the values, initialize to 0 or null.
- 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.
Comments are closed.