Arrays in C Programming Language (One-Dimensional Array)
Arrays in C Programming Language
(One-Dimensional Array)
In today's blog post, we will explore one of the fundamental data structures known as arrays. Arrays play a vital role in a programming language, they allow us to use a single variable that can store multiple values of the same data type. And thus we don't need to make separate variables to store different values. Another important point to keep in mind is that in arrays the elements or values are stored in contiguous memory locations. And also the starting index of arrays is zero.
Arrays are basically fixed sized collection of elements of same datatype. The elements or values are stored at contiguous memory locations. The main advantage or benefit of using arrays is that we dont need to make multiple variables for storing same type of data. We can simply use arrays instead, which is very easy and simple to understand and can be implemented in C programming language.
Why do we use Arrays?
One of the most important points that we already discussed is that arrays can store multiple values in a single variable.
Also, we can use looping statements in order to traverse through arrays and can also process the data easily.
We can also manage the memory easily and efficiently.
Another very important point to keep in mind is that we can also send and receive large collections of data to and from functions respectively.
Advantages of using Arrays:
There are various advantages of using arrays to keep in mind, these points include.
We need less amount of code to access or get data.
We can easily traverse or run through the data that is present in an array using the looping statements.
We can also perform various operations on these array variables i.e. we can perform sorting operations on arrays and can also find the smallest and largest values stored in arrays with very less amount of code.
We can randomly get or access the elements of an array.
Disadvantages of using Arrays:
The biggest and the main disadvantage of using arrays is that the size of arrays is fixed we cannot change its size once it is defined.
Basic syntax and working of Arrays in C programming language
Syntax of array declaration
DataType ArrayName[ArraySize];
Explanation of syntax
Before moving to coding examples in C programming language let us look at the basic syntax and working of arrays in C programming language. The syntax of array declaration is very simple and similar to the declaration of simple C programming variables. First we start with the datatype of the array that whether it is of type int, char, float, double, etc. After that we simply write the name of the array according to our choice. And at last with in the square brackets we define the size of array, which describes how many elements or values can array hold like this,
int NumberArray[5];And remember a very important point that in array the starting index is 0 (zero). Which means that the first value will be stored at the zero location of array.
Simple coding example to understand Arrays in C Programming language
C programming coding example
#include<stdio.h> void main() { int NumArray[5] = { 25, 50, 75, 100, 234 }; for (int i = 0; i < 5; i++) { printf("%d\n", NumArray[i]); } _getch(); }
C programming coding explanation
In the above coding example, we simply declared and initialized an array of size five like this int NumArray[5] = {25, 50, 75, 100, 234};
int NumArray[5] = { 25, 50, 75, 100, 234 };After that, we used a for loop with variable i and placed this variable in place of an Array index value like this NumArray[i],
for (int i = 0; i < 5; i++) { printf("%d\n", NumArray[i]); }
so that we can traverse through the entire array and display the values on the console screen.
C programming program output
25
50
75
100
234
Congratulations! like always we have successfully implemented the arrays in C programming language.
Comments
Post a Comment