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:




61 comments:

  1. Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

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

    ReplyDelete
  3. 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
  4. I see the greatest contents on your blog and I extremely love reading them.

    Data Science Course

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

    Data Science Training

    ReplyDelete
  6. 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
  7. 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
  8. 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
  9. Great information and impressive site found very useful thank you.
    typeerror nonetype object is not subscriptable

    ReplyDelete
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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

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

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

    ReplyDelete
  18. 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
  19. 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
  20. 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

  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  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 do.Data Science Training In Chennai

    Data Science Course In Chennai

    ReplyDelete
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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 Training in Bangalore

    ReplyDelete
  33. A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog.

    Business Analytics Course

    ReplyDelete
  34. I really enjoy every part and have bookmarked you to see the new things you post. Well done for this excellent article. Please keep this work of the same quality.
    Artificial Intelligence course in Chennai

    ReplyDelete
  35. Fantastic blog with excellent information and valuable content just added your blog to my bookmarking sites thank for sharing.
    Data Science Course in Chennai

    ReplyDelete
  36. Movie-watching websites that are more than movie-watching websites Because we are the number 1 free movie site in Thailand for a long time, including new movies, Thai movies, Western movies, Asian movies, we have all kinds of ways for you Including new series Full of all stories without interstitial ads to keep annoying anymore. One place sa-movie.com.

    Android and IOS operating systems. Watch online movies, Thai movies, Western movies, Asian movies, Cartoon movies, Netflix Movie, Action Movies, Comedy Movies, Crime Movies, Drama Movies, Horror Movies, Adventure Movies, Crash Movies and still have many new movies to watch. You can watch for free anytime, anywhere 24 hours a day at see4k.com.


    GangManga read manga, read manga, read manga online for free, fast loading, clear images in HD quality, all titles, anywhere, anytime, on mobile, tablet, computer. Android and IOS operating systems. Read top comics, action dramas, comedy, adventure, horror and manga. New coming every day to watch many more. Can be read for free anytime anywhere 24 hours a day at gangmanga.com..

    It is no secret that football is among the most popular and widely watched sports. Everybody who likes football tries to find the best platform for free soccer streaming. So, what are the best free sports streaming sites? We are going to answer this question. On this page, you can find a detailed overview of the most widespread soccer streaming websites. Keep on reading and make the best choice for you live24th.me.

    ReplyDelete
  37. The truly mind-blowing blog went amazed with the subject they have developed the content. This kind of post is really helpful to gain knowledge of unknown things which surely triggers to motivate and learn the new innovative contents. Hope you deliver the similar successive contents forthcoming as well.

    Cyber Security Course

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

    Machine Learning Course in Bangalore

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

    Digital Marketing Training in Bangalore

    ReplyDelete
  40. I really enjoyed reading your blog. It was very well written and easy to understand. Unlike other blogs that I have read which are actually not very good. Thank you so much!

    Data Science Training in Bangalore

    ReplyDelete
  41. Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts
    Data Science Certification in Hyderabad

    ReplyDelete
  42. Awesome post.Really nice blog, i enjoyed your infomations. Thank you and i will expect more in future..keep it up!!
    Amazon Web Services Training in Chennai

    ReplyDelete
  43. Wow, happy to see this awesome post. I hope this think help any newbie for their awesome work and by the way thanks for share this awesomeness, i thought this was a pretty interesting read when it comes to this topic. Thank you..
    Artificial Intelligence Course

    ReplyDelete
  44. I need to thank you for this very good read and i have bookmarked to check out new things from your post. Thank you very much for sharing such a useful article and will definitely saved and revisit your site.
    Data Science Course

    ReplyDelete
  45. Your site is truly cool and this is an extraordinary moving article and If it's not too much trouble share more like that. Thank You..
    Digital Marketing Course in Hyderabad

    ReplyDelete
  46. Awesome article. I enjoyed reading your articles. this can be really a good scan for me. wanting forward to reading new articles. maintain the nice work!
    Data Science Courses in Bangalore

    ReplyDelete
  47. I am sure it will help many people. Keep up the good work. It's very compelling and I enjoyed browsing the entire blog.
    Business Analytics Course in Bangalore

    ReplyDelete

  48. Excellent Blog! I would like to thank you for the efforts you have made in writing this post. Gained lots of knowledge.
    Data Analytics Course

    ReplyDelete
  49. What an incredible message this is. Truly one of the best posts I have ever seen in my life. Wow, keep it up.
    AI Courses in Bangalore

    ReplyDelete
  50. I have voiced some of the posts on your website now, and I really like your blogging style. I added it to my list of favorite blogging sites and will be back soon ...

    Digital Marketing Training in Bangalore

    ReplyDelete
  51. I found Habit to be a transparent site, a social hub that is a conglomerate of buyers and sellers willing to offer digital advice online at a decent cost.

    Artificial Intelligence Training in Bangalore

    ReplyDelete
  52. Truly incredible blog found to be very impressive due to which the learners who 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 phenomenal content. Hope you arrive with similar content in the future as well.

    Machine Learning Course in Bangalore

    ReplyDelete