In this lesson, you will learn about Array in C, declaring arrays, and manipulating arrays, along with examples to better understand the topic.
An array stores multiple values in one variable; it can retain more than one value at a time. An array in C is an ordered map. A map is a sort associating values with keys such as Array, list (vector), a hash table (a map implementation), dictionary, collection, stack, queue, and probably more.
If you have a list of products (for instance, a list of fruits), it might look like this to store each fruit in a variable:
Fruit1 = "Apple"; Fruit2 = "Banana"; Fruit3 = "Mango"; Fruit4 = "WaterMelon"; Fruit5 = "Dates";
What if your program requires you to loop through the list of fruits and discover a particular one? And what if you have 300 fruits to define? Creating an array is the answer. An array can hold multiple values under a single name, and you can access the values by referring to an index number.
You can create an array using the datatype arrayname[sizeofarray]
DataType arrayName[Sizeofarray];
For example:
int marksOfSubject [5];
In the example above, the Array named marksOfSubject is of int type, and its size is 5. It means marksOfSubject can have five integer types of values.
You can initialize a marksOfSubject array like this.
DataType arrayName[Sizeofarray]= {value1,value2,value3,value4,value5}
Example
int marksOfSubject [5]= {75, 100, 90, 80,60};
See in the table below marksOfSubject Array is of size 5 and if you want to access its element, it will be accessed through the first index by writing syntax marksOfSubject [0] second by marksOfSubject [1], and so on.
pics
// Example of array #include <stdio.h> int main () { //Defining an array marksOfSubject of int type and size of 5 elements int marksOfSubject[5] = { 75, 100, 90, 80, 60 }; printf ("Printing the elements from 1 to 5 \n"); printf ("%d\n", marksOfSubject[0]); printf ("%d\n", marksOfSubject[1]); printf ("%d\n", marksOfSubject[2]); printf ("%d\n", marksOfSubject[3]); printf ("%d\n", marksOfSubject[4]); return 0; }
Output
Printing the elements from 1 to 5 75 100 90 80 60
// Example of array #include <stdio.h> int main () { int marksOfSubject[5] = { 75, 100, 90, 80, 60 }; printf ("Printing marksOfSubject elements\n"); // Loop will print all the marksOfSubject stored for (int var = 0; var < 5; ++var) { printf ("%d\n", marksOfSubject[var]); } return 0; }
Output
Printing marksOfSubject elements 75 100 90 80 60
// Example of array #include <stdio.h> int main () { int marksOfSubject[5]; printf ("Please enter marks of 5 subjects \n"); // Loop will execute 5 times and takes inputs for (int var = 0; var < 5; ++var) { scanf ("%d", &marksOfSubject[var]); // storing the input value in an array } printf ("Printing marksOfSubject elements\n"); // Loop will print all the marksOfSubject stored in above array one by one for (int var = 0; var < 5; ++var) { printf ("%d\n", marksOfSubject[var]); // accessing the array elements } return 0; }
Output
Please enter marks of 5 subjects 8 9 4 1 7 Printing marksOfSubject elements 8 9 4 1 7
Let’s say we have an array of size 5 and you want to access the array elements from that array index 0 to 4. The program will execute correctly. But if you want to access an index 5 that doesn’t exist in memory, then an unexpected behavior will be thrown.
To understand this concept, see the below example. It shows a strange value.
// Example of array out of bound #include <stdio.h> int main () { int marksOfSubject[5] = { 75, 100, 90, 80, 60 }; printf ("Accessing the elements from 1 to 6 \n"); printf ("%d\n", marksOfSubject[0]); printf ("%d\n", marksOfSubject[1]); printf ("%d\n", marksOfSubject[2]); printf ("%d\n", marksOfSubject[3]); printf ("%d\n", marksOfSubject[4]); printf ("%d\n", marksOfSubject[5]); // accessing the index out of bound return 0; }
Output
Accessing the elements from 1 to 6 75 100 90 80 60 32766
Notice the 6th element has a random value that doesn’t not below the array.
In this lesson, you learned about one-dimensional arrays. In the next lesson, you will learn about two-dimensional arrays and their usage.