C Arrays

An array is a continuous and contiguous collection of similar types of data.

Why array?

Before the array, we are using a variable that can store one value at a time and if we need to keep multiple values then we need multiple variables. Let us take an example to suppose that we have five subject scores Hindi – 95, Math – 80, English – 70, Physics – 67, and Chemistry – 59. So we need to hold these values inside five different variables and at the end, we will add them together.


int 
hindi = 95,
math = 80,
english = 70,
physics = 67,
chemistry = 59;

int result = hindi + math + english + physics + chemistry;
printf("%d", result); // 371

This way you will get the correct answer but in long run, this way will be difficult to maintain so you can use an elegant way. here you will use this array to solve this problem.


int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
int total_score_stu_rn1 = 0;

for (int i = 0; i < 5; i++) {
  total_score_stu_rn1 = total_score_stu_rn1 + student_rollno_1[i];
}
printf("%d\n", total_score_stu_rn1); // 371

Don’t worry if you did not understand the above snippet. We will step by step understand the entire snippet.

So first of all understand an array is a special type of variable that can store multiple values. So the syntax for an array is –

Syntax
int nameofarrayvariable[numberofelement] = { comma-separated list of values };

Here int is the type of array that means the array can store the same type of value. second the name of an array variable. [] brackets sign that indicates we are creating an array variable. and last thing in c array variable values store inside {}. So inside the above example, we used the below statement.


int student_rollno_1[5] = { 95, 80, 70, 67, 59 };

Access the elements of an array

This statement indicates we have created a scorecard array variable that can store five values and those five values are inside {}. But here is a catch if you write code like this


int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
printf("%d", student_rollno_1[0]); // 95

That means you are displaying the first value of an array and remembering the first value of the array stored in the 0th index so if we have five elements inside the array then our index will be 0, 1, 2, 3, and 4 and those indexes are the keys to access the value of an array. So now we can print all values of the array like this

Array in C
An array is a continuous and contiguous collection of similar types of data.

int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
printf("%d,", student_rollno_1[0]);
printf("%d,", student_rollno_1[1]);
printf("%d,", student_rollno_1[2]);
printf("%d,", student_rollno_1[3]);
printf("%d ", student_rollno_1[4]);

Change an array element

Now when we stored the wrong score inside a scorecard array like a chemistry subject score then we can replace that value easily with an array index


int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
printf("%d", student_rollno_1[4]); // 59

// replace the 4th index value
student_rollno_1[4] = 79;
printf("%d", student_rollno_1[4]); // 79

Now one very important property of an array is that all values stored inside the array those value are continues and continuous so we can traverse the entire array using an incremented index. and we know that when we use loops that time we also use similar things. So that’s why we are using the below snippet to sum all the values.


int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
int student_rollno_1 = 0;
for (int i = 0; i < 5; i++) {
  student_rollno_1 = student_rollno_1 + student_rollno_1[i];
}
printf("%d", student_rollno_1); // 371

Find length of array

To find the length of an array means to find the total number of elements inside the array. Here we easily find using a simple math trick the total size of the array divided by the size of the first element of an array.


int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
int length = sizeof(student_rollno_1) / sizeof(student_rollno_1[0]);
printf("%d", length); // 5

You May Also Like

About the Author: Pankaj Bisht