What's up guys welcome back to this series and C programming in this video we learn about arrays in C. So far we have been using single variables to store a single data. Now we will learn to store multiple data together inside a single variable using an array.
So let's get started. An array is a collection of similar type of data. Before we learn about array in C let's first know why we need them. Suppose you need to store the a's of 100 people. One way to solve this is to create hundreds of variables to store the a's of each individual.
However, that will be a lengthy and tedious task. Instead, what we can do is create a single array and store the a's of 100 people together. You got that right? Now let's see how we can declare an array in C programming.
On your screen, you can see the syntax for array declaration. Here data type means the type of data that will be stored inside the array. For example, if data type is int, all the elements will be of int type. Array name is an identifier that will be used to identify the array and array size specifies the number of elements that can be stored inside the array.
Let's see an example. I'll type int because I want to store integer data and I'll call my array is and since I want to store the age of 5 people. I have written 5 inside the square bracket.
In the C programming, we cannot change the size and the data type of an array once it is declared. So now the data type of age will be int and it can only store 5 values. Now that we know how to create an array, let's learn to store data in it.
I'll go back to my code editor. Here you can see the code from earlier. I don't need the syntax anymore so I'll just remove this.
So now let's continue with this code. Similar to assigning values to a variable, we use equal to operator to assign data to an array. Now I'll assign five values 21, 29, 25, 32 and 17. These values inside the curly brackets are called array elements. We can also omit the size of the array for that i'll remove this in this case the c compiler automatically determines the size of the array by counting the number of elements inside the curly bracket another thing we can do is assign less number of element than the declared length for example here i'll add 5 inside the square bracket and i'll remove these two elements from the curly bracket you can see the length of array is 5 However, we are assigning only three values to this.
Here the remaining two positions are filled with the default value. In our case, the fourth and fifth position will be 0. Now we know how to assign value to an array. Let's learn to access them. But first, let's learn about the array index.
In C programming, each element of array is associated with a number. This number specifies the position of an array element inside an array and is known as array index and array index starts with 0 so the first element of the array is present at index 0 similarly the second element is present at 1 and so on we use the array index to access array element let me show you here we have our array from our earlier program now let me complete this program so has include stdio dot h int int main and inside the curly bracket return 0 this is the basic structure of C program so I'll cut this and put it here. Now to access an array element we use the array name and the array index inside the square bracket.
So to access the first element of this array we use is square bracket 0. Let me use this and print the first element. So percent d comma is square bracket with the index 0. And I'll run this. As you can see, 21 is printed on the screen. Similarly, we can access second, third, fourth and fifth element by using array indices 1, 2, 3 and 4 respectively. Now what I'll do is I'll copy this.
and I'll paste it here and I'll change the index value from 0 to 1, 2, 3 and 4 and I'll run this. Here you can see all elements are accessed. By the way if you're watching this there is a good chance you want to improve your skills in C programming.
lucky for you we have a mobile app that provides a well-structured c programming course with certification at the end and you can use the app alongside the video to practice on the built-in compiler our course is free so download now by scanning this qr code or click the link in the video description Earlier we learned to assign multiple values to an array during the declaration. We can also assign values individually using the index number. Let's see how we can do that. On your screen you can see the earlier code. Here we have assigned all five values during the declaration.
Since I want to assign values individually, I'll remove this assignment. Now I can assign values individually using the index number. So to assign the first value I can write is square bracket inside the bracket 0 index is equals to 21. Similarly I can assign a second value at the index 1. So is inside the bracket 1 and I'll assign value 29 to this. In this way each value can be assigned to a particular index. So I'll continue on this.
I'll run this code. As you can see each value is assigned to the array. Here this acts as a variable so we can take input from the user and assign to it.
Let's see an example. I'll remove these assignments. Now I'll ask the user for five input value.
So I'll use print statement and I'll ask the user to enter 5 input values and I'll use scanf to store the input values. So scanf bracket inside quotation percent t comma ampersand is square bracket inside the bracket 0 index and scanf %d,& is square bracket inside the bracket 1. Here you can see I have used print statements to print the values. Now I'll run this code.
and I'll enter 21, 29, 25, 32 and 17. As you can see, the input values are now stored inside the array. We can also change the value of an array element. For this we can simply assign a new value to a particular array index. Let's see an example. I'll use the same code from earlier.
Here I'll remove these print statements. Now suppose I want to change this third value to 26 from 25. Here I can simply assign a new value to the position 2. So I will do is square bracket inside the bracket 2 is equals to 26. And I will print this. So percent d comma is square bracket inside the bracket 2. and I'll run this you can see the value at index 2 is 26 okay guys we need your support to keep these types of content free for all users YouTube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's get the engagement score high up so that more people can discover and enjoy these courses In this example, we have used five different scanf statement to take five input values of an array. Also, we have used five different print statement to print values of the array. Here, instead of writing the same code again and again, I can simply use a loop.
I can use one loop to take the input values and second loop to print the values. I'll first add a for loop. As we know array index starts from 0 so int i is equals to 0 so we have started our loop from i is equals to 0 and since the array can store 5 elements So i is less than 5 and we have this update expression that increases the value of i in each iteration by 1. Now inside the loop I will add scanf statement. and I'll use ampersand is with the square bracket inside the bracket we have i. Here you can see each input values is stored in a sequential index from 0 to 4. That's why inside the for loop we have used index i along with the is in the scanf statement.
In each iteration of the loop the value of i is increased by 1 and input value is stored sequentially. Now I can remove this message and these scanf statements. Similarly I can use for loop to print the array elements. So for int i is equals to 0, i is less than 5 and i is less than 5. Now inside the for loop I'll use printf statement %d, is with the square bracket inside the bracket i and I'll remove these print statements. I'll run this program and I'll enter 21, 11, 14, 16 and 17. Here you can see we get the desired output.
Let me show you something interesting. In this code, here I'll change the condition from i less than 5 to i less than 6 and I'll run this code and I'll enter 12, 13, 14, 15 and 16. You can see a random number. So what happens here?
Well we have created the array to store only five elements. Now we can access array elements from index 0 to index 4. However, our for loop runs from i is equals to 0 to 5. So, when the value of i becomes 5, the element is square bracket 5 does not exist. As a result, we get an unexpected value. Sometime, we can also get an error due to this.
So, we should never try to access element of an array outside of its bound or length. Now to revise what we have learned in this video, here is a programming task for you. Create a program that computes the average marks of a student. For this, create an array that stores the marks of 5 subjects.
Compute the total marks by adding all the marks. divide the total marks by total number to the subject and print the average marks. You can find the answer to this question in our GitHub repository and also if you want to revise this concept, all these program in this video will be present in there.
The link is in the description. is in the video description below now that we have reached the end of this video it's time for programming squeeze which value will we get when we print norm square bracket 4 from the following array comment your answer below see you in the next video happy programming