Array in c programming language
Hello Guys, today you are going to learn to Array in a c programming language. Here you can learn how to create an array, initialize an array, and all kinds of Array in a c programming language.
Suppose you are creating a program that stores employees’ names on the computer. Now suppose your company has 200 employees. How do you store the names of these 200 employees? If you are thinking that you will create 200 variables then this will be a very complex approach.
In this, it will take a lot of time to make the program and the program will be much larger than the extent. This is the wastage of both your time and computer memory space. At the same time, you can not think of the names of so many variables, and if you think of them then they cannot remember to use them in the program.
I have an even better approach for you and that approach is called an array in c programming language. An array is a collection of values of a similar type. So with the same type, I mean the same data type, such as int, float, char, etc.
C provides such facility through arrays that you just create a variable and store the name of 200 employees (or whatever information you want to store) in that variable. You might be wondering how to store so many names in a variable. I would be very happy to tell you about it. But before that let’s see how to create an array in c programming language
Creating C Array in c programming language
An array is a structured data type. As you have read in the tutorial C Structures, you can create many variables in one structure. Here too, something is the same. Whenever you create an array in c programming language, you define the name of the array and how many values it is going to store in it.
As if you want to store 5 numbers, you can create an array in c programming language for that. The general structure of creating arrays in C is given below.
data_type array-name[size]; |
By size, you define how many values you want to store. An example of this is being given below.
int num[5]; |
In the above example, the name of the array is num and you can store any 5 integer values.
Initializing C Array in a c programming language
So far I have told you how to create an array in c programming language. Now your most important question is that you will store 5 values in this array. So let me tell you that whenever you create an array, then whatever the size of the array, the same array is allocated to the memory. And those locations are allocated by the same number of index numbers.
Index numbers are a unique name for every location. As if in the example above, there are 5 index numbers. One thing you should always remember is that the index of the array in c programming language always starts with zero.
num[0]
num[1] num[2] num[3] num[4] |
With Array’s name and index number, you can store a value in every location you have created and later you can also value access to it.
(Note: Index of an array in c programming language always starts with zero.)
Like you can insert value in the array created above.
num[0] = 50;
num[1] = 100; num[2] = 150; num[3] = 200; num[4] = 250; |
You can assign all values to each location instead of assigning values to each other at the same time. You can do this in such a way.
int num [5] = {50,100,150,200,250};
And if you want the values to be stored in runtime from the user, then you can do so.
for(int i=0; i<=5; i++)
{ scanf(“%d”,&num[i]); } |
Accessing Array Elements
So far, I have told you to create arrays and store values in them. In this section, you will be asked to access those values. By accessing Array elements you can perform all those operations on them that you can perform with normal variables. For example, you can add 2 array elements.
Wherever you want to access the array element, you type the name of the array in c programming language and the index number. For example, see the below-mentioned statement.
num[2] = num[0] + num [1]; |
The 2 values introduced in the above statement are added by adding to 3 locations.
If you want to print one of the values, you can do this as follows.
printf(“%d”,num[3]); |
The statement given above will print the value of the 4th location, which is 200 in the example given here.
If you want to print the entire array in c programming language together, then you can do this with the help of a loop. Keep in mind that the loop runs as much as the values are in your array. An example of this is being given below.
for(int i=0; i<=5;i++)
{ printf(“%d”,num[i]); } |
Example
#include <stdio.h>
#include <conio.h> int main() { int arr[5]; printf(“Please enter 5 array elements : \n”); for(i=0;i<=4;i++) { scanf(“%d”,arr[i]); } printf(“Array is:”); for(i=0;i<=4;i++) { printf(“%d\t”,arr[i]); } return 0; } |
The above program produces the below-given output.
Please enter 5 array elements : 5 4 3 2 1
Array is : 5 4 3 2 1 |