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:
One Dimensional
Two 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 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 it will hold 10 values.
int Student_Age[5];
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.
Student_Age is the name
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
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 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:
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
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.
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);
In this C array example, We declared the following one
ExampleArray[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
ExampleArray[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+ExampleArray[i]; statement is used to add each and individual 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]
Fourth Iteration: for (i = 3; 3 < 10; 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++)
Seventh Iteration: for (i = 6; 6 < 10; 6++)
Sum= Sum+ ExampleArray[6]
Sum = 210 + 70 = 280
Eighth Iteration: for (i = 7; 7 < 10; 7++)
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
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.
Items in it can be accessed using index value.
Index value starts at 0 and ends at n-1, where n is the size.
C Comments
C Escape Sequence
C Programming Operators
C Arithmetic Operators
C Assignment Operators
C Bitwise Operators
C Conditional Operator
C Increment & Decrement Optr
C Logical Operators
C Relational Operators
C Sizeof Operator
C If Statement
C IF ELSE Statement
C Else If Statement
C Nested If
C Break Statement
C Continue Statement
C Goto Statement
C Switch Case
C For Loop
C While Loop
C Do While Loop
C Arrays
C Two Dimensional Array
C Multi Dimensional Array
C String
C Structures
C Nested Structures
C Array of Structures
C Structures and Functions
C Union
C Structure & Union differences
C Pointers
C Pass Pointers to Functions
C GETS
C fgets Function
C fgetc
C fputs function
C fputc
C Functions
C Types of Functions
C Pass Array to Functions
Call By Value & Call By Reference
C Recursion
C Program Examples