What is Array in C Language | Declaration, Initialization & Example
Today we will learn in detail about what is an array in C Language. What is the uses of array in C and what are the advantages and disadvantages of using array In C Language?
So without wasting time let’s first understand what is Array in C Language.
What is Array In C Language
Array is a group of variables in which all the variables present have the same data type. The group of variables we create through array gets memory in a contiguous fashion and we can easily access each array element by its index number.
Array is a derived data type in which not every array element or variable has a different name, but a name is given to this group of variables and it is accessible from that.
If we want to access any element of the array, then we can access it by using the name of that array and the index number of that element.
Declaration of Array in C
data_type array_Name[arraySize] ;
Example -:
int num[10];
int num[10];
Look at the above example, int is a data type, which shows that in this array we can store only integer type of data and num is the name of the array through which we will access that array and in parenthesis, it is 10 is written, it shows the number of variables, it tells that there is 10 variable in the num array.
Each variable in the array does not have a separate name, but it has an index number. We can easily access the array variable by array name and its index number and can also make any changes in it if we want.
If you want, you can make an array of any size like – If you want to make a group of 100 variables, then you can create a group of 100 variables at once by writing 100 in the array name and its parenthesis. Example – arrayName[100];
Note –:
- The index number of the array starts with zero. Example -: num[0];
- If the size of the array is n then the last index number will be n-1.
Example -: num[5]; | If the size of this array is 5, then its last index number will be 4 num[4].
Suppose if the first index number of the array is 2000, then the index number of the subsequent array will be something like this -: 2004, 2008, 2012, 2016 | Here there is a gap of four bytes in the index number because an int variable consumes 4 bytes of memory.
Initialization of Array in C
We can initialize the array like this -:
Method1 -:
int num[10];
Num[0] = 10;
Num[1] = 20;
Num[2] = 30;
Num[3] = 40;
Num[4] = 50;
Num[5] = 60;
Num[6] = 70;
Num[7] = 80;
Num[8] = 90;
Num[9] = 100;
In this way we can access each individual variable of the array and store the value according to our own.
Method2 -:
Num[10] = {10,20,30,40,50,60,70,80,90,100};
In this, we initialize the value in the array variable only while declaring the array.
Method3 -:
Num[] = {10,20,30,40,50,60,70,80,90,100};
If we want, we can initialize the array in this way also. In this, after the name of the array, without declaring its size, initialize the value in it.
In this method, the size of the array is also considered as the value initialized in it, as in the above array, ten values have been initialized, so the size of this array will be 10.
Example of Array
Let’s now understand Array better with an example.
#include <stdio.h>
void main()
{
int arr[5];
arr[0] = 10;
arr[1] = -10;
arr[2] = 2;
arr[3] = arr[0];
printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
}
Take a look at the above example once, in this, we have created an array of 5 variable size and have initialized some values in them.
In this, we have printed the value of each array element separately with the help of printf() function, whose output is something like this -:
Output -:
10 -10 2 10
Change Value of Array elements
If a value is initialized in the array itself at the time of declaration, then the value of the array can also be changed later.
Example -:
int mark[5] = {19, 10, 8, 17, 9}
mark[2] = -1; // make the value of the third element to -1
mark[4] = 0; // make the value of the fifth element to 0
In this example we have initialized the value in the array at the time of declaration itself, after which we have changed the value of the array element with mark[2] index number by doing mark[2] = -1 also we have mark[4] = The value of the element having index number 4 has also been changed from 0.
Input/Output Array Elements
If we want, we can easily access each array element by array name and its index number and store and print the value in them.
Example -:
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
void main()
{
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i) // taking input and storing it in an array
{
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) // printing elements of an array
{
printf("%d\n", values[i]);
}
}
In this example, we have created an array of 5 variable size named values, in which we have first input the value from the user with the help of for loop and have printed that value later. The output of which came something like this.
Output -:
Enter 5 integers: 20
35
45
50
55
Displaying integers:20
35
45
50
55
Friends, by this time you know what is array and how to use array in C language.
Now Let’s understand that why do we use array in C language, what is the need of array?
Why we need Array in Programming
Array is useful in that condition in which we have to create variables of the same type simultaneously, like if we want to print any 100 numbers by storing them in a variable, then for this we create an array.
This makes the work easy because in this we do not have to think about the name of the variable to store 100 numbers and we can easily create 100 variables in very few lines of code and store the data in them and print them.
But if you want to do this by making a normal variable, then first you have to think the names of 100 variables and then declare them, initialize, etc. It takes more time and effort.
But if you do this work with an array, then you do not have to worry about thinking of names, neither declaration nor initialization.
All these things are done very easily in Array. Array allows us to group multiple variables at once and use them by initializing the values.
Properties of array
Each element of the array has the same size and data type.
Array element gets memory in contiguous fashion if index number of first element starts from 200 then index number of rest element will also proceed in the same order.
We can access each element of the array by its index number and can manipulate it according to our needs.
Advantages of Array
- Array element can be easily accessed by its index number.
- If we have to create a variable to store the data in a group, then we create an Array because our work is done in it in fewer lines of code.
- We can access the element randomly.
- By using this in the loop we can easily access the elements of an array.
- To shorten the array element, we have to write code in very few lines.
Disadvantages of Array
- The size of the array we make at the time of declaration is of the same size till the whole program.
- Size of an array cannot be increased after declaration.
Read More
- Types of Array In C
- String In C
- Structure In C
- Union in C
- Enum in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that after reading this article you get all the information about, what is array in c language and what are the advantages and disadvantages of using the array.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.