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]);

}
Output:
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:




33 comments:

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

    ReplyDelete
  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.

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

    Data Science Course

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

    Data Science Training

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  13. It took a while to understand all the comments, but I really enjoyed the article. It turned out to be really helpful for me and I'm positive for all the reviewers here! It's always nice to be able to not only be informed, I'm sure you enjoyed writing this article.
    Cyber Security Course in Bangalore

    ReplyDelete
  14. Happy to chat on your blog, I feel like I can't wait to read more reliable posts and think we all want to thank many blog posts to share with us.
    Cyber Security Training in Bangalore

    ReplyDelete

  15. Really fantastic and interesting blog enjoyed reading this one waiting for next blog thanks for sharing.
    Data Science Training in Hyderabad

    ReplyDelete
  16. Top quality blog with unique content and information shared was valuable looking forward for next updated thank you
    Ethical Hacking Course in Bangalore

    ReplyDelete
  17. Extraordinary blog went amazed with the content that they have developed in a very descriptive manner. This type of content surely ensures the participants to explore themselves. Hope you deliver the same near the future as well. Gratitude to the blogger for the efforts.

    Digital Marketing Course in Bhilai

    ReplyDelete
  18. Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.

    Data Science training in Bhilai

    ReplyDelete
  19. Honestly speaking this blog is absolutely amazing in learning the subject that is building up the knowledge of every individual and enlarging to develop the skills which can be applied in to practical one. Finally, thanking the blogger to launch more further too.

    Digital Marketing training in Raipur

    ReplyDelete

  20. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
    data science courses

    ReplyDelete
  21. I am glad to post a worthy article about the German Language Course and IELTS Coaching from KCR consultants, this may change your career growth and language skill.
    KCR CONSULTANTS
    KCR CONSULTANTS

    ReplyDelete
  22. Useful article which was very helpful. also interesting and contains good information.
    to know about python training course , use the below link.

    Python Training in chennai

    Python Course in chennai

    ReplyDelete
  23. Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
    Data Analyst Course

    ReplyDelete
  24. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job, Keep it up. Thanks for sharing the content.

    Java Training in Chennai

    Java Course in Chennai

    ReplyDelete
  25. Thanks for this Wounderful Blog. It Was very useful to enhance my knowledge. This is what i exactly need an totally enjoying
    this blog, I Really appreciate everything That you do.Data Science Training In Chennai

    Data Science Course In Chennai

    ReplyDelete
  26. Thanks for this Wounderful Blog. It Was very useful to enhance my knowledge. This is what i exactly need an totally enjoying
    this blog, I Really appreciate everything That you.
    Data Science Training In Chennai
    Data Science Course In Chennai

    ReplyDelete
  27. Really wonderful blog completely enjoyed reading and learning to gain the vast knowledge. Eventually, this blog helps in developing certain skills which in turn helpful in implementing those skills. Thanking the blogger for delivering such a beautiful content and keep posting the contents in upcoming days.

    Data Science training

    ReplyDelete
  28. Truly incredible blog found to be very impressive due to which the learners who ever go through it will try to explore themselves with the content to develop the skills to an extreme level. Eventually, thanking the blogger to come up with such an phenomenal content. Hope you arrive with the similar content in future as well.

    Digital Marketing training

    ReplyDelete
  29. Highly appreciable regarding the uniqueness of the content. This perhaps makes the readers feels excited to get stick to the subject. Certainly, the learners would thank the blogger to come up with the innovative content which keeps the readers to be up to date to stand by the competition. Once again nice blog keep it up and keep sharing the content as always.

    Data Science training in Bhilai

    ReplyDelete
  30. I have to search sites with relevant information ,This is a
    wonderful blog,These type of blog keeps the users interest in
    the website, i am impressed. thank you.
    Data Science Course in Bangalore

    ReplyDelete