Arrays in C Programming [Complete Guide]

arrays in c programming

Before starting this lecture, I'm assuming that you are familiar with Loops in programming language and can use it easily. If you do not then click here before starting with arrays.

Now we start the lecture.

Whenever we want to store data in our program we declare a variable of a particular type and store data in it. Just like if we want to store a number/integer we must declare a variable of int and store the data in it. Just like
int a = 5;
Now the variable a has value of 5

But if we want to store 100 different numbers in our program then what? If we declare different variables a, b, c, d,....... up to our requirement. Then this would be considered as very bad programming. Let if we did this and we want to get numbers from the user or want to print all 100 values that are in different variables, then we have to write 100 variables name and we have to remember all variable names. The code will also become complex to handle.

To overcome this situation arrays were introduced in the programming language. We simply declare an array of particular size and all the values are under one name with different indexes, I will explain how it works.

What is Array?

An array is the data structure consisting of related data items of the same type.

So, an array is the related data item of the same type. One can say "What are related data items? " I would say please wait until the lecture ends.

Now, if we want to store 5 different integers in our program we simply declare an array of integer just like
int myFirstArray[5];
When the upper line runs 5 blocks with the same name (Array name) but different indexes will form in the memory that can store integers in it. As shown in the figure
arrays representation in memory or how arrays can store data under one name
Here, myFirstArray is the name of the array we declare (you can use any name) and square brackets show that this is not a simple variable of an integer but an array of type integer and in square brackets 5 shows that we can store 5 different integers in this array by using indexes from 0 to 4.

To access any block of the array we just need the array name and the index of the block in the square bracket.

Array index always starts from 0 and ends at size-1. We can store elements of the same type of which we declare an array. We have declared an array of the integer of size 5. So we can store values just like
myFirstArray[0] = 56;
myFirstArray[1] = 6;
myFirstArray[2] = 10;
myFirstArray[3] = 25;
myFirstArray[4] = 1;
When this code runs the array will have values in their blocks just like as shown in the figure

array after assigning values

All the values are under one name with different indexes. We just change indexes to access any value in our array. (If an array is of size hundred we don't need to remember hundred variable names but just one name which is of the array name.)

If we want to print 25 from our array we can use statement just like
printf("Value at index 3: %d", myFirstArray[3]);
This will print the value of index 3 just like
Value at index 3: 25

If our array is of small size, we can print and get values manually but this does not actually benefit us. So we use loops for this.
If we want to print our array we have declared and initialized by using loop we just write three lines of code just like

int i;

for(i=0 ; i<=4 ; i++){

printf("Value at index %d: %d \n",i, myFirstArray[i]);

Value at index 0: 56
Value at index 1: 6
Value at index 2: 10
Value at index 3: 25
Value at index 4: 1

If we want to get values from the user in our array we have declared by using loop we simply write for this also

int i;

for(i=0 ; i<=4 ; i++){

printf("Enter Value at index %d : ",i);
scanf("%d", &myFirstArray[i]);


This will get 5 values from the user.

Here the main benefit is that, if we want to get 10 or 100 values from the user our code will remain same, you only need to change the size in the declaration and condition in the loop, nothing else and this will work for more values. So the code will remain relatively small.

In defining, the term related data items are used. why this is used? This tells that we use an array where the whole data are related to each other. For example, marks of a student etc.

Now you would have a better understanding of arrays, how it works and why to use arrays etc.

You can also use Multidimensional arrays in C Programming.

More Topics to read:


  1. Thank you for excellent article.Great information for new guy like antimalware service executable

  2. Biological science assignment writing service seekers have been on the rise lately since most learners need Biological Science Writing Services, biology research paper services and biological science essay writing services.

  3. I see the greatest contents on your blog and I extremely love reading them.

    Data Science Course

  4. I want to post a remark that "The substance of your post is amazing" Great work.

    Data Science Training

  5. I want to thank you for your efforts in writing this article. I look forward to the same best job from you in the future.

    360DigiTMG Data Science Courses

  6. Good blog and absolutely exceptional. You can do a lot better, but I still say it's perfect. Keep doing your best.

    360DigiTMG Data Science Certification

  7. I will be interested in more similar topics. I see you have some really very useful topics, I will always check your blog thank you.

    Artificial Intelligence Course in Bangalore

  8. Great information and impressive site found very useful thank you.
    typeerror nonetype object is not subscriptable

  9. Tremendous blog quite easy to grasp the subject since the content is very simple to understand. Obviously, this helps the participants to engage themselves in to the subject without much difficulty. Hope you further educate the readers in the same manner and keep sharing the content as always you do.

    Data Science Course in Bhilai

  10. Stupendous blog huge applause to the blogger and hoping you to come up with such an extraordinary content in future. Surely, this post will inspire many aspirants who are very keen in gaining the knowledge. Expecting many more contents with lot more curiosity further.

    Digital Marketing Course in Bhilai

  11. Just a shine from you here. I have never expected anything less from you and you have not disappointed me at all. I guess you will continue the quality work.

    Business Analytics Course in Bangalore

  12. The regular visits listed here are the easiest way to appreciate its energy, which is why I go to the website every day for new and interesting information. Thank you so much!

    Data Analytics Course in Bangalore